简体   繁体   English

如何分别在 Matplotlib plot 中的 label 条?

[英]How to individually label bars in Matplotlib plot?

Is there a way to label individual bars using Matplotlib?有没有办法使用 Matplotlib 来 label 个别酒吧? The nice example of a collection of horizontal bars is mosty what I need, however I need to add labels to each bar. 一组水平条的好例子是我最需要的,但是我需要为每个条添加标签。 Eg label the green bar green, orange bar orange, etc. How would I modify the code below to accomplish this task?例如 label 绿色条绿色,橙色条橙色等。我将如何修改下面的代码来完成这项任务?

在此处输入图像描述

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.collections import PolyCollection

data = [    (dt.datetime(2018, 7, 17, 0, 15), dt.datetime(2018, 7, 17, 0, 30), 'sleep'),
            (dt.datetime(2018, 7, 17, 0, 30), dt.datetime(2018, 7, 17, 0, 45), 'eat'),
            (dt.datetime(2018, 7, 17, 0, 45), dt.datetime(2018, 7, 17, 1, 0), 'work'),
            (dt.datetime(2018, 7, 17, 1, 0), dt.datetime(2018, 7, 17, 1, 30), 'sleep'),
            (dt.datetime(2018, 7, 17, 1, 15), dt.datetime(2018, 7, 17, 1, 30), 'eat'), 
            (dt.datetime(2018, 7, 17, 1, 30), dt.datetime(2018, 7, 17, 1, 45), 'work')
        ]

cats = {"sleep" : 1, "eat" : 2, "work" : 3}
colormapping = {"sleep" : "C0", "eat" : "C1", "work" : "C2"}

verts = []
colors = []
for d in data:
    v =  [(mdates.date2num(d[0]), cats[d[2]]-.4),
          (mdates.date2num(d[0]), cats[d[2]]+.4),
          (mdates.date2num(d[1]), cats[d[2]]+.4),
          (mdates.date2num(d[1]), cats[d[2]]-.4),
          (mdates.date2num(d[0]), cats[d[2]]-.4)]
    verts.append(v)
    colors.append(colormapping[d[2]])

bars = PolyCollection(verts, facecolors=colors)

fig, ax = plt.subplots()
ax.add_collection(bars)
ax.autoscale()
loc = mdates.MinuteLocator(byminute=[0,15,30,45])
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(loc))

ax.set_yticks([1,2,3])
ax.set_yticklabels(["sleep", "eat", "work"])
plt.show()

You can use ax.text() in a similar way as you created the rectangles:您可以以与创建矩形类似的方式使用ax.text()

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.collections import PolyCollection

data = [(dt.datetime(2018, 7, 17, 0, 15), dt.datetime(2018, 7, 17, 0, 30), 'sleep'),
        (dt.datetime(2018, 7, 17, 0, 30), dt.datetime(2018, 7, 17, 0, 45), 'eat'),
        (dt.datetime(2018, 7, 17, 0, 45), dt.datetime(2018, 7, 17, 1, 0), 'work'),
        (dt.datetime(2018, 7, 17, 1, 0), dt.datetime(2018, 7, 17, 1, 30), 'sleep'),
        (dt.datetime(2018, 7, 17, 1, 15), dt.datetime(2018, 7, 17, 1, 30), 'eat'),
        (dt.datetime(2018, 7, 17, 1, 30), dt.datetime(2018, 7, 17, 1, 45), 'work')]

cats = {"sleep": 1, "eat": 2, "work": 3}
colormapping = {"sleep": "C0", "eat": "C1", "work": "C2"}

verts = []
colors = []
for start, end, cat in data:
    v = [(mdates.date2num(start), cats[cat] - .4),
         (mdates.date2num(start), cats[cat] + .4),
         (mdates.date2num(end), cats[cat] + .4),
         (mdates.date2num(end), cats[cat] - .4),
         (mdates.date2num(start), cats[cat] - .4)]
    verts.append(v)
    colors.append(colormapping[cat])

bars = PolyCollection(verts, facecolors=colors)

fig, ax = plt.subplots()
ax.add_collection(bars)

for start, end, cat in data:
    ax.text((mdates.date2num(start) + mdates.date2num(end)) / 2, cats[cat], cat,
            color='lightgoldenrodyellow', fontsize=15, ha='center', va='center')

ax.autoscale()
loc = mdates.MinuteLocator(byminute=[0, 15, 30, 45])
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(loc))

ax.set_yticks([1, 2, 3])
ax.set_yticklabels(["sleep", "eat", "work"])
plt.show()

结果图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM