簡體   English   中英

如何在 Matplotlib 中繪制重復的圖例

[英]How to plot duplicates legends in Matplotlib

我有一個帶有重復圖例 (yLabels) 的 Pandas 數據框,每個圖例都有不同的值 (yValues)。 問題是當我使用 Matplotlib 繪制這個數據框時,所有重復的圖例都被分組 - 這不是我的意圖。 我必須顯示重復的圖例,每個圖例都有其特定的價值。

我怎樣才能做到這一點?

代碼:

import matplotlib.pyplot as plt
import pandas as pd


while True:

    yLabels = ['ABC', 'ABC', 'ABC'] ### these must appear 3x in the legends
    yValues = [-0.15, 0.00, 0.23]

    df=pd.DataFrame({'x': yLabels, 'Goal': [0,0,0], 'y': yValues})

    plt.style.use('seaborn-darkgrid')
    for column in df.drop('x', axis=1):
    plt.plot(df['x'], df[column], marker='', color='grey', linewidth=2, alpha=0.4)
    plt.plot(df['x'], df['Goal'], marker='', color='orange', linewidth=4, alpha=0.7)
    plt.xlim(-0.5,3)
num=0
    for i in df.values[2][1:]:
        num+=1
        name=list(df)[num]
    plt.text(10.2, df.Goal.tail(1), 'Goal', horizontalalignment='left', size='small', color='orange')
    plt.title("Distance between the Goal and the Actual Rates differences", loc='left', fontsize=12, fontweight=0, color='orange')
    plt.xlabel("Shipments")
    plt.ylabel("Variation")

    plt.pause(5)
    plt.clf()
    plt.cla()

謝謝

IIUC,你正在尋找這樣的東西:

yLabels = ['ABC', 'ABC', 'ABC'] ### these must appear 3x in the legends
yValues = [-0.15, 0.00, 0.23]

df = pd.DataFrame({'x': yLabels, 
                   'Goal': [0, 0, 0], 
                   'y': yValues})

plt.scatter(df.index, df["y"])
plt.xticks(df.index, df["x"])
plt.show()

在這里,我使用行索引作為沿 x 軸的數據點位置,並用df["x"]列標記它們。

非常無格式的 matplotlib 圖

另見: https : //matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html

暫無
暫無

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

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