python 图例颜色与图表颜色不匹配

46scxncf  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(420)

我已经绘制了数据,但图例颜色与图形不同。

for row in Survey2.values.tolist():
    values = row[1:] + [row[1]]
    plt.polar(angles, values, 'o-', linewidth=0.5, label=row[0])
    plt.fill(angles, values, alpha=0.25)

    
# Representation of the spider graph
plt.legend(Survey2["createdAt"], 
           loc=4, 
           fontsize=7)
           
plt.title('PHQ-9,GAD-7 and Likert Scale over the study period', size=12, y=1.07)
plt.thetagrids(angles * 180 / np.pi, labels)
plt.figure(figsize=(10, 10))
plt.show()

enter image description here

k97glaaz

k97glaaz1#

每行绘制两个元素:

  1. plt.polar(...
  2. plt.fill(...阴影
    因此,添加图例时,每行数据将获得两项:一个是点,一个是填充。图例和你的图一样的颜色。图例只有3个条目,但是有6条曲线
    您可以通过将label='_nolegend_'添加到plt.fill(来避免这种情况,如以下类似问题所示:Show only certain items in legend Python Matplotlib
    另外,如果在创建曲线时添加了标签(使用label=row[0]),为什么在创建图例时添加新标签Survey2["createdAt"]?这将覆盖之前给出的标签

相关问题