1

I have looked at the following thread and it has helped, but I was wondering if anyone had attempted it in a slightly different way.

Plotting a horizontal line on multiple subplots in python using pyplot

Below is what the data looks like so you can test (if necessary) = ctr_df

Snippet of the data can be found below:

Date        LAL_3%      Core        Page_1      Homepage
4/06/2017   0.288142    0.947993    1.174094    0.26622
11/06/2017  0.270052    0.919648    0.615855    0.206031
18/06/2017  0.260225    0.941274    0.550324    0.230775
25/06/2017  0.345208    0.867322    0.373997    0.205385
2/07/2017   0.318578    1.687208    0.420367    0.235314
9/07/2017   0.45653     1.227451    0.71128     0.455536
16/07/2017  0.543492    1.086996    0.415289    0.44763
23/07/2017  0.503581    1.010364    0.642291    0.317182
30/07/2017  0.373479    0.918334    0.580428    0.282783

My code is below:

ctr_df.plot(kind='bar', sharey=False, sharex=True, subplots=True, 
                                    legend=True, width=0.8, layout=(2,2), fontsize=12, grid=True, figsize=(20,12), colormap='Dark2')

plt.axhline(0.3, color='k', linestyle='--')
plt.tight_layout()
plt.show()

This returns exactly what I want (individual subplots by audience, by week), however, the axhline is only appearing on the last plot, not all of them.

Can this be done using my approach? I can't find anything to suggest it can. If this is not possible with this approach, that's fine, I can use the above thread to help revise my code.

Thanks

AdrianC
  • 383
  • 4
  • 18
  • Why can't you implement the linked solution? Please note that nobody can reproduce your problem, because this is not a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Mr. T May 19 '18 at 03:58
  • I can implement the linked solution - I was curious to see if this could be done another way. Subplots=True is faster for me. I have added a picture of the data if you wanted to test it yourself. Like I said, if it can be done, great, if not, then it isn't a big deal. I just like looking at different ways. – AdrianC May 19 '18 at 04:05
  • Do you seriously expect someone to type your data by hand from the picture? – Mad Physicist May 19 '18 at 04:08
  • Apologies - I have updated if that helps. – AdrianC May 19 '18 at 04:16

1 Answers1

2

You use pandas plotting functions, which are rather constructed for convenience than full matplotlib support. Afaik, there is no way to directly construct horizontal lines in all subplots. So back to matplotlib:

from matplotlib import pyplot as plt
import pandas as pd

ctr_df = pd.read_csv("test.csv", delim_whitespace = True,index_col = "Date")
#plot and retrieve axes
axes = ctr_df.plot(kind='bar', sharey=False, sharex=True, subplots=True, 
                                    legend=True, width=0.8, layout=(3,2), fontsize=12, grid=True, figsize=(20,12), colormap='Dark2')
#plot horizontal line in each subplot 
for ax in axes.flatten():
    ax.axhline(0.3, color='k', linestyle='--')
plt.tight_layout()
plt.show()

Output:

enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54