我想显示不同的图形。为此,我必须在它们之间切换。
figure(1),一张图,应该包含 for 循环中的所有行 图(2),每次循环迭代一个图
plt.figure(1)
plt.subplots(dpi=96)
plt.subplots_adjust(right=0.75)
for #anything... :
#load new line values...
plt.figure(2)
ax = plt.subplots(dpi=96)
twin1 = plt.twinx()
plt.figure(1)
plt.plot(x, y, ".-")
plt.figure(2)
ax.plot(x, y, ".-")
twin1.plot(x2, y2, ".-")
plt.title('single plot for evere line')
ax.set(xlabel='...')
twin1.yaxis.label.set_color('r')
twin1.set(ylabel='y')
ax.grid(visible = True,
which = 'major',
axis = 'x',
linewidth = 0.4
)
plt.tight_layout()
plt.show()
plt.figure(1)
plt.tight_layout()
plt.show()```
There are some examples in the web. But figure(2) uses the twinx, and because of this it don't works. What I'm doing wrong?
@TrentonMcKinney,好的,我还没有很清楚地看到隐式和显式 API 的区别。但现在我读了文档。我现在用显式 API 尝试了它,我与一些属性作斗争...... :) 但现在它看起来不错。
谢谢推荐!
这是新编写的代码示例,可能对其他用户有帮助:
x = [1,2,3,4]
y1_all = [[1.1, 1.2, 1.3 ,1.4],[2.1, 2.2, 2.3 ,2.4]]
y2 = [10, 12, 13, 13.3]
fig1 = plt.figure()
fig1.set_dpi(96)
fig1ax = fig1.subplots()
fig1ax.set_title('fig1: all lines')
for y in y1_all:
fig2 = plt.figure()
fig2.set_dpi(96)
fig2ax = fig2.subplots()
fig2twin1 = fig2ax.twinx()
fig1ax.plot(x, y, ".-")
fig2ax.plot(x, y, ".-")
fig2twin1.plot(x, y2, ".-r")
fig2ax.set_title('fig2: single line')
fig2ax.set(xlabel='...')
fig2twin1.yaxis.label.set_color('r')
fig2twin1.set(ylabel='fig2 y2 axis')
fig2ax.grid(visible = True,
which = 'major',
axis = 'x',
linewidth = 0.4
)
fig2ax.grid(visible = True,
which = 'major',
axis = 'y',
color = 'm',
linewidth = 0.4
)
fig2twin1.grid(visible = True,
which = 'major',
axis = 'y',
color = 'r',
linewidth = 0.4
)
fig2.tight_layout()
fig2.show()
fig1.tight_layout()
fig1.show()
你还有什么改进建议吗?