设置标记大小时,图例不会显示大小可变的标记

2024-04-25 09:35:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我的绘图中的图例不显示具有可变大小的标记,而仅显示不具有可变大小的标记。 我无法共享我的数据集。(看图片) Hided marker in the legend plot,以及它的代码

    fig = plt.figure(figsize=(40, 10))
ax = fig.add_subplot(111)

ax.scatter(c.consumption_kW[c.ch1_state=='rise'],c.chiller1[c.ch1_state=='rise'],
           marker='^',label='Rise',c='blue',s=c['ch1_slope']*4)

ax.scatter(c.consumption_kW[c.ch1_state=='fall'],c.chiller1[c.ch1_state=='fall'],
           marker='v',label='Fall',c='red',s=c['ch1_slope']*4)

ax.scatter(c.consumption_kW[c.ch1_state=='stable'],c.chiller1[c.ch1_state=='stable'],
           marker='o',label='Stable',c='green')

ax.legend()

如果我不改变标记的大小,问题就不会发生,但绘图的意义就不大了 no sized marker plot,以及它的代码

 fig = plt.figure(figsize=(40, 10))
ax = fig.add_subplot(111)

ax.scatter(c.consumption_kW[c.ch1_state=='rise'],c.chiller1[c.ch1_state=='rise'],
           marker='^',label='Rise',c='blue')

ax.scatter(c.consumption_kW[c.ch1_state=='fall'],c.chiller1[c.ch1_state=='fall'],
           marker='v',label='Fall',c='red')

ax.scatter(c.consumption_kW[c.ch1_state=='stable'],c.chiller1[c.ch1_state=='stable'],
           marker='o',label='Stable',c='green')

ax.legend()

帮帮我。 提前多谢。:)


Tags: 标记figaxmarkerlabelstatestablerise
1条回答
网友
1楼 · 发布于 2024-04-25 09:35:45

您可以通过访问legendHandles来手动设置图例标记的大小。下面是基于您的代码的非常简单的示例

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(40, 10))
ax = fig.add_subplot(111)

x=range(10)
y=[a*5 for a in x]
z=[a*10 for a in x]

ax.scatter(x, y, marker='^',label='Rise',c='blue',s=x*4)
ax.scatter(y, z, marker='v',label='Fall',c='red',s=y*4)
ax.scatter(x, z, marker='o',label='Stable',c='green')

leg=ax.legend()
# Set the marker size of the first row to 20 and the second row to 30
leg.legendHandles[0].set_sizes([20])
leg.legendHandles[1].set_sizes([30])
plt.show()

结果就在这里。 enter image description here

相关问题 更多 >