0

I have gathered a code to make plots from data from multiple days. I have a data file containing over 40 days and 19k timestamps, and I need a plot, one for each day. I want python to generate them as different plots.

Mr. T helped me a lot with providing the code, but I cannot manage the code to get it to plot individual plots instead of all in one subplot. Can somebody help me with this?

Picture shows the current output:

Output

My code:

import matplotlib.pyplot as plt
import numpy as np
 
#read your data and create datetime index
df= pd.read_csv('test-februari.csv', sep=";") 
df.index = pd.to_datetime(df["Date"]+df["Time"].str[:-5], format="%Y:%m:%d %H:%M:%S")

#group by date and hour, count entries
dfcounts = df.groupby([df.index.date, df.index.hour]).size().reset_index()
dfcounts.columns = ["Date", "Hour", "Count"]
maxcount = dfcounts.Count.max()

#group by date for plotting
dfplot = dfcounts.groupby(dfcounts.Date)

#plot each day into its own subplot
fig, axs = plt.subplots(dfplot.ngroups, figsize=(6,8))

for i, groupdate in enumerate(dfplot.groups):
    ax=axs[i]
    #the marker is not really necessary but has been added in case there is just one entry per day
    ax.plot(dfplot.get_group(groupdate).Hour, dfplot.get_group(groupdate).Count, color="blue", marker="o")
    ax.set_title(str(groupdate))
    ax.set_xlim(0, 24)
    ax.set_ylim(0, maxcount * 1.1)
    ax.xaxis.set_ticks(np.arange(0, 25, 2))

plt.tight_layout()
plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
peter
  • 21
  • 4

1 Answers1

1

Welcome to the Stackoverflow.

Instead of creating multiple subplots, you can create a figure on the fly and plot onto it in every loop separately. And at the end show all of them at the same time.

for groupdate in dfplot.groups:
    fig = plt.figure()
    plt.plot(groupdate.Hour, groupdate.Count, color="blue", marker="o")
    plt.title(str(groupdate))
    plt.xlim(0, 24)
    plt.ylim(0, maxcount * 1.1)
    plt.xticks(np.arange(0, 25, 2))

plt.tight_layout()
plt.show()
enesdemirag
  • 325
  • 1
  • 10
  • Thanks! @enesdemirag. Can I also change the size (max y value) depending on the max of the particular plot? – peter Nov 23 '20 at 10:09
  • @peter yup. Default limit of the y-axis is already a bit higher than the maximum value. You can also add ```plt.ylim()``` to scale it however you want. – enesdemirag Nov 23 '20 at 10:20
  • Thank you, I only have the maximum of all the graphs set as max value. I want the max value of every individual graph. Is there a simple function for that? Some plots go to 20, some to 80. If I want it to change along with the plot itself? – peter Nov 23 '20 at 11:26
  • That was deliberate, peter, for comparability among graphs. Remove `plt.ylim()` from the loop and matplotlib automatically scales the y-axis. However, the changing y-axis scales might deceive your eye and lead to the wrong conclusions. – Mr. T Nov 23 '20 at 11:34
  • The OP started off with the more explicit object-oriented interface to matplotlib, so I think the answer should too. – Paul H Nov 23 '20 at 15:45