1

I have a 2x2 graph with date in x-axis in both graphs. I have used datetime.strptime to bring a string into type = datetime.datetime object format.

However I am planning to have some 12 subplots and doing this the following way seems messy.

Is there a better 'pythonic' way?

This is what I have:

 xx.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
 plt.grid(True)
 plt.ylabel('paramA',fontsize=8, color = "blue")
 plt.tick_params(axis='both', which='major', labelsize=8)
 plt.plot(date_list, myarray[:,0], '-b', label='paramA')
 plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment

 xx = plt.subplot(2,1,2)
 xx.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
 plt.grid(True)
 plt.ylabel('paramB', 'amount of virtual mem',fontsize=8, color = "blue")
 plt.tick_params(axis='both', which='major', labelsize=8)
 plt.plot(date_list, myarray[:,1], '-y', label='paramB')plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment ```

PS: Initially I tried defining the plot as follows. This however did not work:

fig, axs = plt.subplots(2,1,figsize=(15,15)) 
plt.title('My graph')     
for ax in enumerate(axs):
   ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
4
  • Are you just looking for a for loop to do it for all 12 subplots? Please explain more what exactly you want Commented May 24, 2019 at 11:41
  • I could not get them in a for loop. Also I wanted to use the ConciseDateFormatter in matplotlib.dates. Both gave me Attribute error inspiting of importing all the necessary modules Commented May 24, 2019 at 11:51
  • I answer below how to use a for loop. For rest of your questions, provide a Minimal, Complete, and Verifiable example Commented May 24, 2019 at 11:52
  • Check my edited answer below Commented May 24, 2019 at 12:11

1 Answer 1

3

You failed to provide any data or a Minimal, Complete, and Verifiable example. Nevertheless, something like this should work. You can extend it to your real case by using desired number of rows and columns in the first command.

fig, axes = plt.subplots(nrows=2, ncols=3)
labels = ['paramA', 'paramB', 'paramC', 'paramD', 'paramE', 'paramF']

for i, ax in enumerate(axes.flatten()):
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
    ax.grid(True)
    ax.set_ylabel(labels[i], fontsize=8, color="blue")
    ax.tick_params(axis='both', which='major', labelsize=8)
    ax.plot(date_list, myarray[:,i], '-b', label=labels[i])
    plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment

EDIT:

Change your code to

fig, axs = plt.subplots(2,1,figsize=(15,15)) 
plt.title('My graph')     
for ax in axs:
   ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, just updated the page to see your answer. What is the axes.flatten() doing? Maybe that was why my code did not work
@tweet: axes here is a 2-d object because you have multiple rows and columns in plt.subplots(). axes.flatten() converts this 2-d object into a 1-d array of axis objects which can be used in a for loop to plot one at a time. How many rows and columns do you have in the figure?
@tweet: I got it. You don;t need enumerate or flatten because you just have a single column of figures

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.