【问题标题】:How to add vertical grid lines to a matplotlib chart?如何将垂直网格线添加到 matplotlib 图表?
【发布时间】:2019-11-04 17:19:23
【问题描述】:

我有以下代码

import matplotlib.pyplot as plt
import numpy as np

[...]

    def display(self):
        t = self.stateLabels

        fig, axs = plt.subplots(4, 1, sharex=True)
        fig.subplots_adjust(hspace=0)

        axs[0].plot(t, self.production)
        axs[0].title.set_text('Production')
        axs[0].frameon = True

        axs[1].plot(t, self.consumption)
        axs[1].title.set_text('Consumption')

        axs[2].plot(t, self.investment)
        axs[2].title.set_text('Investment')

        axs[3].plot(t, self.gdp)
        axs[3].title.set_text('GDP')

        plt.show()

这将创建以下图表:

我想在轴上添加垂直线,使图表看起来像这样:

有可能吗?如果是,怎么做?

【问题讨论】:

  • 试试plt.gca().yaxis.grid(True)
  • 谢谢,但没用。
  • 好像是这个的副本:stackoverflow.com/questions/16074392/…,但您似乎想在任意位置绘制垂直线?
  • @rcriii 感谢您的评论。不,它们不是任意位置。在 x 轴上,每个值都有刻度(Step 0 是一个刻度,Step 1 是另一个刻度,等等。)我希望这些刻度更高,以便我可以看到在哪里一步结束,另一步开始。

标签: python python-3.x matplotlib


【解决方案1】:

ax.axvline 上查看 matplotlib 文档 这应该使您能够在指定的 x 位置以及指定的起点和终点绘制垂直线。

【讨论】:

  • 这种方式可行,但 OP 必须为每个子图多次调用它。
  • 是的,不幸的是,相对于当前子图,y 位置仅从 0 到 1。
猜你喜欢
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 2013-04-11
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多