在所有子图中绘制水平线

plot horizontal line in all subplots

我在 python 工作,并找到了几个解决方案。但它需要创建每个单独的子图。但是因为有一个参数你可以做 subplot=True,我想知道有没有办法用一行代码来做......你怎么说 sharey=True,你能做到吗"share"水平常量?

我一直在玩弄它。最开始只出现在最后一张图上,现在完全不显示了。

import matplotlib.pyplot as plt

line_up_points.plot(subplots=True, layout=(3, 3),sharey=True, figsize=(18, 12))
plt.legend(loc='best')
plt.axhline(y=125.08, color='r')

显示的内容如下:

但我想在 y=125.08

的每个子图中都有一条水平线

如果不单独创建 7 个不同的图表,有什么想法吗?

如果我没记错的话你应该得到一个轴对象矩阵。

这应该可以解决问题:

axes = line_up_points.plot(subplots=True, layout=(3, 3),sharey=True, figsize=(18, 12))

for c in axes:
   for ax in c:
      ax.axhline(y=125.08, color='r')

这是一个完整的例子:

%matplotlib inline  # For Jupyter Notebooks
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10, 7))

axes = df.plot(subplots=True, layout=(3, 3), figsize=(16,9))

for c in axes:
    for ax in c:
        ax.axhline(y=0.5, color='r')