首页 > 解决方案 > 首先“分组”,然后从熊猫中绘制/保存为 png

问题描述

首先我需要过滤数据然后分别绘制每个组并将文件保存到目录

for id in df["set"].unique():

  df2= df.loc[df["set"] == id]

   outpath = "path/of/your/folder/"


   sns.set_style("whitegrid", {'grid.linestyle': '-'})
   plt.figure(figsize=(12,8))
   ax1=sns.scatterplot(data=df2, x="x", y="y", hue="result",markers=['x'],s=1000)
   ax1.get_legend().remove()
   ax1.set_yticks((0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5), minor=False)
   ax1.set_xticks([0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.6], minor=False)

   fig = ax1.get_figure()
   fig.savefig(path.join(outpath,"id.png",dpi=300 )

标签: pandasmatplotlibplot

解决方案


这对我有用,但速度很慢

groups = df.groupby("set") 


for name, group in groups: 

    
    sns.set_style("whitegrid", {'grid.linestyle': '-'})
    plt.figure(figsize=(12,8))
    
    ax1=sns.scatterplot(data=group, x="x", y="y", hue="result",markers=['x'],s=1000)
    ax1.get_legend().remove()
    ax1.set_yticks((0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5), minor=False)
    ax1.set_xticks([0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.6], minor=False)
    
    fig = ax1.get_figure()
    fig.savefig("directory/{0}.png".format(name), dpi=300)
    

推荐阅读