簡體   English   中英

Lineplot - 為奇數子圖繪制單個圖例

[英]Lineplot - plot a single legend for uneven number of subplots

我正在繪制下圖,我想在其中繪制適用於所有人的單個圖例,本質上這將是一個小框,其中藍色線為 AB = 0,綠色線為 AB = 1。

此外,我正在使用plt.subplot(...因為它可能必須處理奇數列來繪制。

我試着把它放在盒子外面,它在任何地方都看不到。

陰謀

plt.figure(figsize=(16,10))
plt.subplots_adjust(hspace=0.3)
plt.suptitle("Some title", fontsize=18, y=0.95)
plt.style.use('seaborn-darkgrid')
for i, col in enumerate(tms_0.columns):
    ax = plt.subplot(3,4,i+1)
    ax.plot(tms_0.index, tms_0[col], label=col, color='skyblue')
    ax.plot(tms_1.index, tms_1[col], label=col, color='green')
    #plt.legend(loc='upper left')
    #ax.set_title(col.upper())
    ax.set_xticks([])
fig.legend(["X", "Y"], loc='lower right', bbox_to_anchor=(1,-0.1), ncol=2, bbox_transform=fig.transFigure)
plt.show()

此代碼中的col實際上是數據框中的一列,因此我不能以正常方式使用它,這就是我在set_title中使用它的原因。

我發現了一些基於這個線程的選項如何手動創建圖例

legend_elements = [plt.Line2D([0], [0], color='skyblue', lw=2.5, label='ClientAB=0'),
                   plt.Line2D([0], [0], color='green', lw=2.5, label='ClientAB=1')]
ax.legend(handles=legend_elements, bbox_to_anchor=(1.2, 4.05))

暫無
暫無

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

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