6

I need to plot non-numeric data against dates as a simple line graph. I am using matplotlib.

Here's some sample code.

import matplotlib.pyplot as plt

xticks=['Jan','Feb','Mar','April','May']
x=[1,2,3,4,5]
yticks = ['Windy', 'Sunny', 'Rainy', 'Cloudy', 'Snowy']
y=[2,1,3,5,4]

plt.plot(x,y,'bo') #.2,.1,.7,.8
plt.subplots_adjust(left =0.2)

plt.xticks(x,xticks)
plt.yticks(y,yticks)
plt.show()

Graph generated from code

I want to start the tick labels leaving some space from the origin. So that they don't look very crammed. Should I be using Fixedlocator for this? Also I would like the graph to be a line showing markers for every point like in this example. How can I achieve this?

Sam
  • 7,252
  • 16
  • 46
  • 65
Angela
  • 1,671
  • 3
  • 19
  • 29
  • 1
    If you're interested in cheap hacks, using `plt.xticks(x,['\n' + s for s in xticks])` works – wim Jan 19 '12 at 02:58

1 Answers1

9
  • You can "lift" the graph by setting a lower ylim with ax.set_ylim.
  • Marker dots can be added to the plot using the marker = 'o' parameter setting in the call to plt.plot:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

xticks=['Jan','Feb','Mar','April','May']
x=[1,2,3,4,5]
yticks = ['Windy', 'Sunny', 'Rainy', 'Cloudy', 'Snowy']
y=[2,1,3,5,4]

plt.plot(x,y,'b-', marker = 'o') #.2,.1,.7,.8
plt.subplots_adjust(left =0.2)

plt.xticks(x,xticks)
plt.yticks(y,yticks)
ax.set_ylim(0.5,max(y))
plt.show()

enter image description here

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thanks for that. Works well. Also added ax.set_xlim(0.5,max(x)) to leave some space for the ticks on the x axis. – Angela Jan 19 '12 at 05:56