正在生成重影图

2024-10-04 03:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

fig = plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)
ax2 = ax1.twinx()
num =  list111.lt(-90).sum(1)
plt.yticks(fontsize = 25)
ax = num.plot(figsize=(45,25), ax=ax2, color = 'Red')
df2.plot(y = 'Close', figsize=(45,25), ax=ax1, color = 'Green')
ax1.grid()
ax.margins(x=0)

我试图在同一个图表中绘制ax1和ax2。我得到的是一个鬼阴谋:

enter image description here

如何摆脱第二个重影打印并将带有标签的x轴移动到顶部打印


Tags: 代码plotfigpltaxnumcolorfigure
1条回答
网友
1楼 · 发布于 2024-10-04 03:26:02

声明

ax2 = plt.subplot(212, sharex = ax1)

生成位于ax1子地块下方的子地块。但这与声明相矛盾

ax2 = ax1.twinx()

指向ax1轴上的次y轴

如果希望所有数据仅绘制在单个轴上,可以删除第一条语句并使用.twinx()方法:

ax1 = plt.axes()
ax2 = ax1.twinx()
# remaining code

否则,将两个轴分别与一起使用

ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)
# remaining code

相关问题 更多 >