【发布时间】:2020-02-29 09:09:00
【问题描述】:
我认为问这个问题可能很愚蠢,但我在谷歌上找不到任何匹配我的问题的解决方案。希望有人能帮忙。
简单地说,我想在同一个图上绘制不同的函数,而不是子图,因为我发现大多数在线解决方案都使用子图来做到这一点。我的目的是为了更好地比较不同的数据线在同一个图上相同的情节,但具有不同的颜色和不同的图例。我有 2 个数字,因为第二个数字只是具有相同想法的其他东西。
我尝试使用以下代码。但是每次循环都会覆盖子图。我不想删除旧行。我希望每个循环都分别更新图 1 和图 2,而不擦除之前的数据线。
fig1 = plt.figure(1)
fig2 = plt.figure(2)
for diffSteps,listpair in fixed_table_plots.items():
ax1 = fig1.add_subplot(1,1,1)
ax1.scatter(listpair[0],listpair[1],s=2000,label="error_step_"+str(diffTableSteps))
ax1.legend()
plt.show()
ax2 = fig2.add_subplot(1,1,1)
accuracy_bw = [x - math.log(y,2) for x,y in zip(listpair[0],listpair[1])]
ax2.scatter(listpair[0],accuracy_bw,s=200,label="accuracy bw_step_"+str(diffTableSteps))
ax2.set_xlabel("input int bw")
ax2.set_ylabel("total bw accuracy")
ax2.legend()
plt.show()
非常感谢任何帮助!
更新:
for diffSteps,listpair in fixed_table_plots.items():
plt.figure(1)
ax1.scatter(listpair[0],listpair[1],s=2000,label="error_step_"+str(diffTableSteps))
ax1.legend()
plt.figure(2)
accuracy_bw = [x - math.log(y,2) for x,y in zip(listpair[0],listpair[1])]
ax2.scatter(listpair[0],accuracy_bw,s=200,label="accuracy bw_step_"+str(diffTableSteps))
ax2.set_xlabel("input int bw")
ax2.set_ylabel("total bw accuracy")
这对我来说效果很好,唯一的问题是 plt.show() 不能在中间调用。有没有办法动态显示图?重新激活情节也感觉很奇怪。如果这是一个好方法,请告诉我...
谢谢!
【问题讨论】:
-
@finefoot 我不知道。我刚试过。我认为我犯的一个错误是调用 plt.show() 会清除所有内容。
标签: python matplotlib