【问题标题】:Python Adding Totals to Plot with MatplotlibPython 使用 Matplotlib 将总计添加到绘图中
【发布时间】:2012-06-17 15:30:13
【问题描述】:

有没有办法将每日总数添加到使用 matplotlib 制作的图表中。这是我目前创建的:

我想做的是为每一天插入总计,例如:“每日总计是当天所有小时值的总和。

目前我的数据在一个 csv 中,这里有一天的价值:

2012-02-13 05:00:00,65217
2012-02-13 06:00:00,82418
2012-02-13 07:00:00,71316
2012-02-13 08:00:00,66833
2012-02-13 09:00:00,69406
2012-02-13 10:00:00,76422
2012-02-13 11:00:00,94188
2012-02-13 12:00:00,111817
2012-02-13 13:00:00,127002
2012-02-13 14:00:00,141099
2012-02-13 15:00:00,147830
2012-02-13 16:00:00,136330
2012-02-13 17:00:00,122252
2012-02-13 18:00:00,118619
2012-02-13 19:00:00,115763
2012-02-13 20:00:00,121393
2012-02-13 21:00:00,130022
2012-02-13 22:00:00,137658
2012-02-13 23:00:00,139363

我正在绘制它:

data = csv2rec('temp.csv', names=['time', 'values'])

rcParams['figure.figsize'] = 12, 6
rcParams['font.size'] = 8

fig = plt.figure()

plt.plot(data['time'], data['value'])

ax = fig.add_subplot(111)
ax.plot(data['time'], data['value'])
days = mdates.DayLocator()
fmt = mdates.DateFormatter('%D')
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(fmt)

fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left')

ax.grid()

plt.savefig(output_name)

可能有更好的方法来做到这一点,但我的想法是将 csv 中的每日值求和并将它们存储在一个数组中,使用带有某种小函数的“图例”来确定位置并将这些将数组中的值放入其中。 ??这听起来对还是有人有更好的方法?

【问题讨论】:

    标签: python csv matplotlib


    【解决方案1】:

    确定 matplotlib 是一个类似 matlab 的库。它提供了为注释编写文本的能力。如下图所示(比你想要的还要复杂):

    可以在此处找到包含源代码的文档页面:

    http://matplotlib.sourceforge.net/api/pyplot_api.html

    http://matplotlib.sourceforge.net/users/annotations.html

    http://matplotlib.sourceforge.net/users/text_props.html

    【讨论】:

    • 所以我认为我会根据axis.text对齐向每个区域添加注释。这似乎相当简单。
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多