matplotlib 如何为多个相同的标签显示一个图例

qlvxas9a  于 2023-11-22  发布在  其他
关注(0)|答案(2)|浏览(139)

下面是绘制一个时间序列图,其中颜色类别的年龄组和标签类别的位置。

import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.use('TkAgg')  # !IMPORTANT

fig, ax = plt.subplots()
ax.plot([93497, 137558, 158293, 180411, 183457, 187016, 189596],
        marker='.', linestyle='-', linewidth=0.5, label='Hong Kong', color="bisque")
ax.plot([67594, 112486, 130558, 149890, 157317, 163262, 165828],
        marker='v', linestyle='-', label='Kowloon', color="bisque")
ax.plot([58093, 102680, 121633, 138414, 144665, 149776, 152765],
        marker='*', linestyle='-', label='New Territories', color="bisque")

ax.plot([101779, 140103, 160860, 176330, 183330, 182458, 184591],
        marker='.', markersize=5, linestyle='-', linewidth=0.5, label='Hong Kong', color="teal")
ax.plot([81941, 115792, 131061, 147161, 153582, 160379, 161225],
        marker='v', markersize=5, linestyle='-', label='Kowloon', color="teal")
ax.plot([56305, 91942, 106554, 120067, 125490, 132070, 136451],
        marker='*', markersize=5, linestyle='-', label='New Territories', color="teal")
ax.set_ylabel('$HKD/sq. m.')
ax.legend()
plt.show()

字符串
测试结果:


的数据
如何使每个标签只有一个图例,因此没有重复的图例名称。
例如,代替“香港”,“九龙”,“新界”,“香港”,“九龙”,“新界”
只显示“香港”、“九龙”、“新界”。

xvw2m8pv

xvw2m8pv1#

答案:https://stackoverflow.com/a/41765095/6660373
试着按你的需要调整。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.legend_handler import HandlerBase

class AnyObjectHandler(HandlerBase):
    def create_artists(self, legend, orig_handle,
                       x0, y0, width, height, fontsize, trans):
        print(orig_handle)
        l1 = plt.Line2D([x0,y0+width], [0.9*height,0.9*height], markersize=10,
                           linestyle=None, color="bisque", marker=orig_handle[0])
        l2 = plt.Line2D([x0,y0+width], [0.1*height,0.1*height], 
                           color="teal", marker=orig_handle[0])
        return [l1, l2]

x = np.linspace(0, 3)
fig, axL = plt.subplots(figsize=(4,3))
axR = axL
axM = axR

axL.plot([93497, 137558, 158293, 180411, 183457, 187016, 189596],
        marker='.', linestyle='-', linewidth=0.5, label='Hong Kong', color="bisque")
axR.plot([67594, 112486, 130558, 149890, 157317, 163262, 165828],
        marker='v', linestyle='-', label='Kowloon', color="bisque")
axM.plot([58093, 102680, 121633, 138414, 144665, 149776, 152765],
        marker='*', linestyle='-', label='New Territories', color="bisque")

axL.plot([101779, 140103, 160860, 176330, 183330, 182458, 184591],
        marker='.', markersize=5, linestyle='-', linewidth=0.5, label='Hong Kong', color="teal")
axR.plot([81941, 115792, 131061, 147161, 153582, 160379, 161225],
        marker='v', markersize=5, linestyle='-', label='Kowloon', color="teal")
axM.plot([56305, 91942, 106554, 120067, 125490, 132070, 136451],
        marker='*', markersize=5, linestyle='-', label='New Territories', color="teal")

plt.legend([(".",), ("v",), ("*",)], ['Hong Kong', 'Kowloon', 'New Territories'],
           handler_map={tuple: AnyObjectHandler()})

plt.show()

字符串


的数据

mtb9vblg

mtb9vblg2#

你只需要把label=None放在你不想在图例中的行。下面的示例代码:

import matplotlib.pyplot as plt
import matplotlib as mpl

# mpl.use('TkAgg')  # !IMPORTANT

fig, ax = plt.subplots()
ax.plot([93497, 137558, 158293, 180411, 183457, 187016, 189596],
        marker='.', linestyle='-', linewidth=0.5, label='Hong Kong', color="bisque")
ax.plot([67594, 112486, 130558, 149890, 157317, 163262, 165828],
        marker='v', linestyle='-', label='Kowloon', color="bisque")
ax.plot([58093, 102680, 121633, 138414, 144665, 149776, 152765],
        marker='*', linestyle='-', label='New Territories', color="bisque")

ax.plot([101779, 140103, 160860, 176330, 183330, 182458, 184591],
        marker='.', markersize=5, linestyle='-', linewidth=0.5, label=None, color="teal")
ax.plot([81941, 115792, 131061, 147161, 153582, 160379, 161225],
        marker='v', markersize=5, linestyle='-', label=None, color="teal")
ax.plot([56305, 91942, 106554, 120067, 125490, 132070, 136451],
        marker='*', markersize=5, linestyle='-', label=None, color="teal")
ax.set_ylabel('$HKD/sq. m.')
ax.legend()
plt.show()

字符串
输出量:


的数据

相关问题