Python Forum

Full Version: Fitting xticks on x-axis
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

[attachment=631]

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.
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()