Python Forum
Fitting xticks on x-axis - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Fitting xticks on x-axis (/thread-18175.html)



Fitting xticks on x-axis - schniefen - May-08-2019

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.


RE: Fitting xticks on x-axis - heiner55 - May-28-2019

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