Python画散点图之seaborn

    技术2022-08-16  65

    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')

     

    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()

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    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()

     

    本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。

     

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

    Processed: 0.021, SQL: 9