简体   繁体   中英

plot horizontal line in all subplots

I;m working in python and found a couple solutions to do this. But it requires creating each individual subplot. But since there's a parameter where you could do subplot=True , I'm wondering is there a way to to do it in one line of code...much how you could say sharey=True , can you make it "share" a horizontal constant?

I've been playing around with it. At first, it was only showing up on the last graph, but now it's not showing at all.

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')

Here's what it's showing:

在此输入图像描述

But I would like to have a horizontal line on each of those subplots at y=125.08

Any idea without individually creating 7 different graphs?

If I'm not mistaken then you should get back a matrix of axis objects.

This should do the trick:

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')

Here's a full example:

%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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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