【问题标题】:Unable to show legend on the graph for each subplot无法在图表上显示每个子图的图例
【发布时间】:2022-11-29 12:41:40
【问题描述】:

我正在尝试显示每个子图的图例,但它似乎不起作用。子图的右上角有一个框,但它是空的。这是我试过的代码。还有我可以放大第一个子图吗?

XWD_TO = data.iloc[:,0:1]
VAS_AX = data.iloc[:,1:2]
BTC_AUD = data.iloc[:,2:]

# Daily data chart of XWD.TO and VAS.AX (need to fix legend)
#fig = plt.figure()
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
fig.suptitle('Daily Volatility')
ax1 = plt.subplot(211)
ax1.set_title("XWD.TO and VAS.AX")
ax1.plot(XWD_TO,c='orange')
ax1.plot(VAS_AX,c='green')
ax1.legend(loc = "upper right")
ax2 = plt.subplot(212)
ax2.set_title("BTC-AUD")
ax2.plot(BTC_AUD,c='blue')
plt.show()

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    你需要标签每个子图。我已经稍微编辑了你的代码。希望能帮助到你。

    XWD_TO = data.iloc[:,0:1]
    VAS_AX = data.iloc[:,1:2]
    BTC_AUD = data.iloc[:,2:]
    
    # Daily data chart of XWD.TO and VAS.AX (need to fix legend)
    #fig = plt.figure()
    fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
    fig.suptitle('Daily Volatility')
    ax1 = plt.subplot(211)
    ax1.set_title("XWD.TO and VAS.AX")
    ax1.plot(XWD_TO,c='orange',label='XWD_TO')
    ax1.plot(VAS_AX,c='green',label='VAS_AX'))
    ax1.legend(loc = "upper right")
    ax2 = plt.subplot(212)
    ax2.set_title("BTC-AUD")
    ax2.plot(BTC_AUD,c='blue',label='BTC_AUD')
    ax2.legend(loc = "upper right")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多