pandas - 首先“分组”,然后从熊猫中绘制/保存为 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 )
解决方案
这对我有用,但速度很慢
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)
推荐阅读
- flutter - Flutter NestedScrollView 没有立即显示浮动的 SliverAppBar?
- java - 我不明白为什么对于高于 49 的输入(如 54)会有一个
用于倒数第四个输出打印(共 10 个) - c - 全局/本地/静态互斥体之间的区别?
- javascript - 为什么 scrollTo 在我的 ClickListener 中有效,但在 AfterViewInit 中无效?
- css - 这个 css 兄弟选择器是如何工作的?
- sql - “不是一个有效的月份”,即使其他日期类型操作有效
- c# - c#从多个线程将文本写入一个文本文件
- mongodb - 为什么 MongoDB 连接只需要 Root CA?
- javascript - 未评估条件时,Chartjs 禁用图例单击不起作用
- c# - .net core 3.1 和 Angular 与 Signalr 之间的连接期间出现 Websocket 错误