天天看點

Python畫散點圖之seaborn

1.散點圖。

import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

p1=sns.regplot(x=df["sepal_length"], y=df["sepal_width"])
plt.show()

# 儲存圖檔
fig = p1.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#40_Scatterplot_with_regression_fit_seaborn.png')
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

p2=sns.regplot(x=df["sepal_length"], y=df["sepal_width"], fit_reg=False)
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

p1=sns.regplot(x=df["sepal_length"], y=df["sepal_width"], marker="+", fit_reg=False)
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = sns.load_dataset('iris')
p1=sns.regplot(x=df["sepal_length"], y=df["sepal_width"], marker=3, fit_reg=False)
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

p2=sns.regplot(x=df["sepal_length"], y=df["sepal_width"], fit_reg=False, scatter_kws={"color":"darkred","alpha":0.3,"s":200} )
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

p1=sns.regplot(x=df["sepal_length"], y=df["sepal_width"], line_kws={"color":"r","alpha":0.7,"lw":5})
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# 使用hue進行分類
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False)
# 設定圖例
plt.legend(loc='lower right')
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# 使用hue進行分類
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False,  markers=["o", "x", "1"])
# 設定圖例
plt.legend(loc='lower right')
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False,  palette="Set2")

plt.legend(loc='lower right')
plt.show()
           
Python畫散點圖之seaborn
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False,  palette=dict(setosa="#9b59b6", virginica="#3498db", versicolor="#95a5a6"))

plt.legend(loc='lower right')
plt.show()
           
Python畫散點圖之seaborn
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

np.random.seed(0)

# 設定資料
df = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])
value=(df['x']>0.2) & (df['y']>0.4)
df['color']= np.where( value==True , "#9b59b6", "#3498db")


p1=sns.regplot(data=df, x="x", y="y", fit_reg=False, scatter_kws{'facecolors':df['color']})
plt.show()
           
Python畫散點圖之seaborn
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

# 設定資料
df = pd.DataFrame({
    'x': [1, 1.5, 3, 4, 5], 
    'y': [5, 15, 5, 10, 2],
    'group': ['A','other group','B','C','D']
    })

p1=sns.regplot(data=df, x="x", y="y", fit_reg=False, marker="+", color="skyblue")
plt.show()
           
Python畫散點圖之seaborn
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

# 設定資料
df = pd.DataFrame({
    'x': [1, 1.5, 3, 4, 5], 
    'y': [5, 15, 5, 10, 2],
    'group': ['A','other group','B','C','D']
    })

p1=sns.regplot(data=df, x="x", y="y", fit_reg=False, marker="o", color="skyblue", scatter_kws={'s':400})
p1.text(3+0.2, 4.5, "An annotation", horizontalalignment='left', size='medium', color='black', weight='semibold')
plt.show()
           
Python畫散點圖之seaborn
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

# 設定資料
df = pd.DataFrame({
    'x': [1, 1.5, 3, 4, 5], 
    'y': [5, 15, 5, 10, 2],
    'group': ['A','other group','B','C','D']
    })

p1=sns.regplot(data=df, x="x", y="y", fit_reg=False, marker="o", color="skyblue", scatter_kws={'s':400})
for line in range(0,df.shape[0]):
    p1.text(df.x[line]+0.2, df.y[line], df.group[line], horizontalalignment='left', size='medium', color='black', weight='semibold')
plt.show()
           
Python畫散點圖之seaborn

本部落客新開公衆号, 希望大家能掃碼關注一下,十分感謝大家。

Python畫散點圖之seaborn

本文來自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py