簡體   English   中英

為DataFrame的兩個子圖制作單個圖例

[英]Make single legend for two subplots of DataFrame

我在兩個不同的子圖上創建了兩個軸的圖。 當前,一個覆蓋另一個。 問題在於使圖例在堆棧中包含兩個標簽。 我怎樣才能做到這一點?

d = data.groupby('atemp_rounded').sum().reset_index()
fig = plt.figure()
ax1 = fig.add_subplot(111) # don't know what 111 stands for...
ax2 = ax1.twinx()
d.plot(ax=ax1, y='casual')
d.plot(ax=ax2, y='registered', color='g')
plt.show()

壞傳說

您可以設置各個圖的圖例,而創建一個圖圖例。 要將其放置在軸邊界內,需要在軸坐標中指定位置。

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"A" : [3,2,1], "B" : [2,2,1]})

fig = plt.figure()
ax1 = fig.add_subplot(111) # don't know what 111 stands for...
ax2 = ax1.twinx()
df.plot(ax=ax1, y='A', legend=False)
df.plot(ax=ax2, y='B', color='g', legend=False)
fig.legend(loc="upper right", bbox_to_anchor=(0,0,1,1), bbox_transform=ax1.transAxes)
plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM