【问题标题】:Customize Legend Matplotlib自定义图例 Matplotlib
【发布时间】:2022-12-11 14:45:28
【问题描述】:

我有这样的镜头地图可视化:

我使用的代码如下所示:

pitch = VerticalPitch(half=True, goal_type='box',linewidth=1,line_color='white',pad_bottom= 0.21)
fig, ax = pitch.draw(figsize=(9, 8))
# hteam goals:
hteam_sc_g = pitch.scatter(hteam_df_g["start_location_x"], hteam_df_g["start_location_y"], s = hteam_df_g["xG"]*300+100,marker="o",c= 'red', ax=ax, alpha=0.7, edgecolors='red',linewidth = 1, label = " Goals")

# hteam non goals shots:
hteam_sc_ng = pitch.scatter(hteam_df_ng["start_location_x"], hteam_df_ng["start_location_y"],s = hteam_df_ng["xG"]*300+100,marker="o", c= 'none', ax=ax, edgecolors='cyan',linewidth = 1, label = " Shots")

fig.text(s=hteam + " Shots Map", x= 0.06, y= 0.95, fontsize=20,fontweight="bold")
fig.text(s='Final Champions League 2010/2011', x = 0.06, y=0.9, fontsize=15)

# Legend
for xG in [50, 100,200, 400]:
    plt.scatter([], [], c='none',edgecolors='gray',linewidth = 1, alpha=1, s=xG, label= 'xG')
ax.legend(title = 'Outcome', labelspacing=1, loc="upper center",ncol=6,frameon=True,fancybox=True, shadow=True,bbox_to_anchor=(0.5, -0.01))

我想尝试自定义我的图例以显示如下内容:

首先,我尝试的是使用 xG 中的删除标签 plt.scatter([], [], c='none',edgecolors='gray',linewidth = 1, alpha=1, s=xG, label= False) 但结果看起来像我用文本“False”发送的图像

【问题讨论】:

    标签: python pandas matplotlib visualization legend


    【解决方案1】:

    要删除图例中的文本“False”,您只需将标签设置为只有一个空格的字符串,label = " " 而不是label = False。这样,您的圈子将出现在图例中,而不会伴随任何文字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2021-07-14
      • 2021-03-23
      相关资源
      最近更新 更多