我有一个带有重复图例(yLabels)的熊猫数据框,每个图例都有不同的值(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()
谢谢
答案 0 :(得分:0)
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"]
列对其进行了标记。
另请参阅:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html