Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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?

like image 618
chitown88 Avatar asked Mar 07 '23 08:03

chitown88


1 Answers

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')
like image 183
Stefan Falk Avatar answered Mar 20 '23 14:03

Stefan Falk