【问题标题】:Position of legend in matplot with secondary y-axis (python)图例在matplot中的位置与辅助y轴(python)
【发布时间】:2022-01-06 10:12:38
【问题描述】:

我尝试使用 matplotlib 在 python 中创建一个图,该图由两个图和一个具有辅助 y 轴的图组成。第一个是散点图,第二个是线图。 现在,我想将图例移动到其他地方,但每当我使用 ax.legend() 时,只会出现第一个轴的标签,而第二个轴的标签会消失。

import pandas as pd

# the data:
data = pd.DataFrame(
    {'Date': {0: '2021-01-01 18:00:00', 1: '2021-01-02 20:00:00', 2: '2021-01-03 19:00:00', 3: '2021-01-04 17:00:00', 4: '2021-01-05 18:00:00', 5: '2021-01-06 18:00:00', 6: '2021-01-07 21:00:00', 7: '2021-01-08 16:00:00'},
     'Value A': {0: 107.6, 1: 86.0, 2: 17.3, 3: 56.8, 4: 30.0, 5: 78.3, 6: 110.1, 7: 59.0},
     'Value B': {0: 17.7, 1: 14.1, 2: 77.4, 3: 4.9, 4: 44.4, 5: 28.3, 6: 22.9, 7: 83.2},
     'Value C': {0: 0.0, 1: 0.5, 2: 0.3, 3: 0.6, 4: 0.9, 5: np.nan, 6: 0.1, 7: 0.8},
     'Value D': {0: 0.5, 1: 0.7, 2: 0.1, 3: 0.5, 4: 0.4, 5: 0.8, 6: 0.8, 7: 0.8},
     'Flag': {0: 1, 1: np.nan, 2: np.nan, 3: np.nan, 4: np.nan, 5: np.nan, 6: 1, 7: np.nan}})
data["Date"] = pd.to_datetime(data["Date"])

# plot the flagged points as dots
data_flag = data[data["Flag"] == True]
ax = data_flag.plot.scatter(x="Date",
                            y="Value A",
                            c="black",
                            label="Outlier")

# plot the series as lines
ax = data.plot(x="Date",
               y=["Value A",
                  "Value B",
                  "Value C",
                  "Value D"
                  ],
               secondary_y=["Value C", "Value D"],
               ax=ax
               )

ax.legend(bbox_to_anchor=(1.1, 1.05))

plt.show()

输出看起来像这样,辅助轴的图例中缺少标签

没有ax.legend(),“Value C”和“Value D”的标签是图例的一部分。

一次移动所有标签的最佳方法是什么?

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    这种方法(使用您提供的数据):

    # plot the flagged points as dots
    data_flag = data[data["Flag"] == True]
    ax = data_flag.plot.scatter(x="Date",
                                y="Value A",
                                c="black",
                                label="Outlier")
    
    # plot the series as lines
    ax = data.plot(x="Date",
                   y=["Value A",
                      "Value B",
                   ],
                   secondary_y=False,
                   ax=ax
                   )
    
    ax2 = data.plot(x="Date",
                   y=["Value C",
                      "Value D"
                      ],
                   secondary_y=True,
                   ax=ax
                   )
    
    ax.legend(bbox_to_anchor=(1.1, 1.1), loc="upper left")
    ax.set_ylabel("Value A, Value B")
    ax2.legend(bbox_to_anchor=(1.1, 0.85), loc="upper left")
    ax2.set_ylabel("Value C, Value D")
    
    
    plt.show()
    

    生成了三个图,一个用于标记的异常值,一个用于Value A, Value B,最后一个带有辅助 y 轴的Value C, Value D。如下所示:

    【讨论】:

    • 谢谢!这似乎可以解决问题。有两个图例并手动调整它们的位置是可以接受的,因为无论如何我都必须找到最佳位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多