【问题标题】:Ghost plot being generated正在生成的鬼图
【发布时间】:2020-09-27 19:47:25
【问题描述】:

我有以下代码:

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。我得到的是一个鬼情节:

我怎样才能摆脱第二个鬼图并将带有标签的 x 轴移动到顶部图?

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    声明

    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
    

    【讨论】:

      猜你喜欢
      • 2016-06-26
      • 2011-02-13
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2020-04-03
      • 2011-11-18
      • 1970-01-01
      相关资源
      最近更新 更多