在 matplotlib 中向日期格式的时间序列添加垂直线

2024-01-27

我正在尝试向时间序列图中添加一条红色垂直线,其中 x 轴格式为 %Y-%m-%d。我想添加该行的日期是 2013 年 5 月 14 日。只需在“plt.show()”之前添加一行:

plt.axvline(x=2013-05-14)

or:

plt.axvline(x='2013-05-14')

返回错误:

RuntimeError: RRuleLocator estimated to generate 23972 ticks from 0044-05-12 23:59:59.999990+00:00 to 2013-06-07 00:00:00.000010+00:00: exceeds Locator.MAXTICKS * 2 (2000) 

这是该函数,它运行良好:

 def time_series(self):
    fig = plt.figure(figsize=(20, 20), frameon = False)
    ax1 = fig.add_subplot(3, 1, 1)


    d_dates, d_flux_n2o, d_sem_n2o = np.loadtxt('%stime_series/dynamic.csv' %self.dir['r_directory'],  delimiter = ',', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d')})

    ax1.set_xlabel('Date')
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    ax1.xaxis.set_major_locator(mdates.MonthLocator())
    ax1.xaxis.set_minor_locator(mdates.DayLocator())
    plt.gcf().autofmt_xdate()

    ax1.errorbar(d_dates, d_flux_n2o, yerr=d_sem_n2o, fmt="y-", linewidth=1.5, label = 'Biodynamic')
    ax1.legend(loc = 0)

    plt.show()

你必须给出axvline方法是数值,而不是字符串。您可以通过定义一个转换器来实现此目的,该转换器将日期的字符串表示形式转换为datenums。您的计算机中已经有一个这样的转换器np.loadtxt方法调用。如果将其定义为函数,则可以在加载数据时使用它,也可以用于单个日期字符串。

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np


def time_series(self):
    fig = plt.figure(figsize=(20, 20), frameon = False)
    ax1 = fig.add_subplot(3, 1, 1)

    # Converter to convert date strings to datetime objects
    conv = np.vectorize(mdates.strpdate2num('%Y-%m-%d'))


    d_dates, d_flux_n2o, d_sem_n2o = np.loadtxt('%stime_series/dynamic.csv' %self.dir['r_directory'],  delimiter = ',', unpack=True, converters={0: conv})

    ax1.errorbar(d_dates, d_flux_n2o, yerr=d_sem_n2o, fmt="y-", linewidth=1.5, label = 'Biodynamic')
    ax1.legend(loc = 0)
    ax1.axvline(conv('2013-05-14'), color='r', zorder=0)

    ax1.set_xlabel('Date')
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    ax1.xaxis.set_major_locator(mdates.MonthLocator())
    ax1.xaxis.set_minor_locator(mdates.DayLocator())
    plt.gcf().autofmt_xdate()

    plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 matplotlib 中向日期格式的时间序列添加垂直线 的相关文章

  • 如何抑制 pyinstaller 生成的可执行文件窗口中的所有警告

    我已经使用 pyinstaller 从 python 文件生成了可执行文件 该程序按其应有的方式工作 但在我想隐藏的窗口中出现了一条警告消息 当 python 文件在 IDE 中运行时 以下行会抑制所有警告消息 warnings filte
  • 使用 Pandas 和 Group By 绘制堆叠直方图

    我正在使用如下所示的数据集 Gender Height Width Male 23 4 4 4 Female 45 4 4 5 我想可视化高度和宽度的堆叠直方图 我希望每个图有两个堆叠的直方图 每个性别一个 这是文档中的堆叠直方图 如果存在
  • 在子图中显示多个图像

    如何使用 matplotlib 函数 plt imshow image 显示多个图像 例如我的代码如下 for file in images process file def process filename image mpimg imr
  • matplotlib:渲染到缓冲区/访问像素数据

    我想使用 matplotlib 生成的图作为 OpenGL 中的纹理 到目前为止 我遇到的 matplotlib 的 OpenGL 后端要么不成熟 要么已经停止使用 所以我想避免使用它们 我当前的方法是将图形保存到临时 png 文件中 并从
  • matplotlib 中 plt.plot() 的第三个参数有什么作用?

    plt plot 通常接受 x y 和格式 但是 如果我将第三个参数作为数据传递 则会绘制另一条线 我无法理解其中的关系 x np linspace 0 10 5 plt plot x x x label linear plt grid C
  • 我可以使用文本叠加执行类似 imsave() 的操作吗?

    我在用imsave 依次制作许多 PNG 我将它们合并为 AVI 并且我想添加移动文本注释 我用ImageJ http imagej nih gov ij features html制作 AVI 或 GIF 我不需要轴 数字 边框或任何东西
  • 在一张图中同时绘制两个截面强度

    我有一个形状数组 512 512 看起来像 行 x 列 y 密度 z 数组的数量 0 012825 0 020408 0 022976 0 015938 0 02165 0 024357 0 036332 0 031904 0 025462
  • 根据统计数据获取cassandra中的数据点

    我正在测试 Cassandra 2 0 作为存储时间序列数据的可能替代品 我制作了一个简单的表并将一些数据转储到其中 CREATE TABLE DataRaw channelId int sampleTime timestamp value
  • ggplot多个时间不等的时间序列

    我知道有一些与时间序列和多个数据帧相关的已回答问题 但我似乎无法弄清楚这一点 我想绘制 4 个不同压力传感器与时间的时间戳数据 pa 列 我有来自同一实验的 4 个带时间戳的压力读数 dfs 然而 由于传感器故障和数据中的其他信号 传感器收
  • 如何在seaborn中绘制离散变量的分布图

    当我画画的时候displot对于离散变量 分布可能不像我想象的那样 例如 We can find that there are crevices in the barplot so that the curve in kdeplot is
  • Matplotlib:检查空图

    我有一个循环加载并绘制一些数据 如下所示 import os import numpy as np import matplotlib pyplot as plt for filename in filenames plt figure i
  • matplotlib 中的固定大小矩形?

    Is there any way to plot a rectangle spanning the entire length of the X axis but with a fixed and static height of say
  • Matplotlib:将默认图例 loc 和图例 bbox 更改为锚点

    每次用 pandas 在 matplotlib 中绘图时 为了将图例放在图形之外 我必须执行以下操作 single obj trading curve instrument toppercentile plot legend loc cen
  • 在 Python 的 matplotlib 中使用时间序列的百分位数设置颜色渐变

    我有一个时间序列 在一年的数组大小 365 10000 中 变量的每日值将超过 10 000 个 因为我将拥有大量数据 许多变量的许多时间序列 所以我希望仅保存百分位数 0 10 20 90 100 并在稍后的图中使用它们来设置颜色渐变显示
  • 使用 numpy 和 matplotlib 绘制总和直方图,而不是计数

    我有一些每行两列的数据 就我而言 工作提交时间和区域 我使用 matplotlib 的 hist 函数生成一个图表 其中 x 轴上按天划分时间 y 轴上按天计数 import numpy as np import matplotlib py
  • 无法使用 API 获取的数据初始化 ngx-charts

    我在尝试初始化使用构建的图表时遇到了一些困难ngx 图表 https github com swimlane ngx charts使用API 获取数据 我构建了一个 REST API 在正确调用后 它会输出一些时间序列数据 prices i
  • 给定一般 3D 平面方程

    假设我有一个 3D 平面方程 ax by cz d 我如何在 python matplotlib 中绘制它 我看到一些例子使用plot surface 但它接受 x y z 值作为二维数组 我不明白如何将我的方程转换为参数输入plot su
  • Python 3.x 中的绘图

    在Python 2 6中 我使用matplotlib制作了一些简单的图表 但是 它与 Python 3 1 不兼容 有哪些替代模块可以完成相同的事情而不非常复杂 您说您想创建一些简单的图表 但没有真正说明您想要多简单或哪种类型的图表 只要它
  • matplotlib 中没有边缘的阴影矩形补丁

    当尝试将带有填充图案的矩形面片添加到绘图中时 在指定填充值时似乎无法将关键字参数 edgecolor 设置为 none 换句话说 我试图添加一个没有边缘但有图案填充的阴影矩形 这似乎不起作用 仅当我还允许在矩形面片周围绘制边缘时 该图案才会
  • Python - matplotlib - 加权图

    是否可以绘制weighted graphs在 python 中使用 matplotlib 我在 matplotlib 网站上没有找到它 如果我不能使用matplotlib 我可以用其他东西来做吗 我们可以做吗libreoffice 本质上这

随机推荐