简体   繁体   English

如何在 Matplotlib 中绘制重复的图例

[英]How to plot duplicates legends in Matplotlib

I have a Pandas dataframe with duplicated legends (yLabels) and each of them with a different value (yValues).我有一个带有重复图例 (yLabels) 的 Pandas 数据框,每个图例都有不同的值 (yValues)。 The problem is that when I plot this dataframe using Matplotlib, all the duplicated legends are grouped - and this is not my intention.问题是当我使用 Matplotlib 绘制这个数据框时,所有重复的图例都被分组 - 这不是我的意图。 I have to show duplicated legends, each of them with its specific value.我必须显示重复的图例,每个图例都有其特定的价值。

How can I do this?我怎样才能做到这一点?

CODE:代码:

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()

Thank you谢谢

IIUC, you are looking for something like this: 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()

Here, I have used the row index as the data point location along the x axis, and have labeled them with the df["x"] column.在这里,我使用行索引作为沿 x 轴的数据点位置,并用df["x"]列标记它们。

非常无格式的 matplotlib 图

See also: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html另见: 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