Python Forum
Fitting xticks on x-axis
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fitting xticks on x-axis
#1
I'm working on a plot of energy consumption versus month over a period of years.

import matplotlib.pyplot as pat
kwh=[88736,92100,95420,98161,100326,...,327384]
yearmonthday=['2003-01-01','2003-02-01','2003-03-01','2003-04-01',...,'2015-06-01']

plt.plot(range(len(kwh)),kwh)
plt.xticks(range(len(yearmonthday)),yearmonthday)
plt.title('Energy consumption 2003-2015')
plt.ylabel('Kilowatthour')
plt.figure(figsize=(10,6))
plt.savefig('Plotx.jpg')
plt.show()
The current output of the plot looks something like this.

   

I would like to add the dates as ticks on the x-axis, yet this gets very tight. The command plt.figure(figsize=(10,6)) doesn't really seem to work, since whenever I change it the saved figure is a blank one.
Reply
#2
You can rotate the xticks:

#!/usr/bin/python3
import matplotlib.pyplot as plt

kwh=[88736,92100,95420,98161,100326,327384]
yearmonthday=['2003-01-01','2003-02-01','2003-03-01','2003-04-01','2015-06-01']
.
plt.plot(range(len(kwh)),kwh)

plt.xticks(range(len(yearmonthday)),yearmonthday,rotation='vertical')
plt.title('Energy consumption 2003-2015')
plt.ylabel('Kilowatthour')
plt.savefig('Plotx.jpg')

plt.show()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020