MikeHoss
MikeHoss

Reputation: 1487

Space between Y-axis and First X tick

Matplotlib newbie here.

I have the following code:

from pylab import figure, show
import numpy

fig = figure()
ax = fig.add_subplot(111)

plot_data=[1.7,1.7,1.7,1.54,1.52]

xdata = range(len(plot_data))

labels = ["2009-June","2009-Dec","2010-June","2010-Dec","2011-June"]

ax.plot(xdata,plot_data,"b-")

ax.set_xticks(range(len(labels)))
ax.set_xticklabels(labels)

ax.set_yticks([1.4,1.6,1.8])

fig.canvas.draw()

show()

When you run that code, the resulting chart has a run-in with the first tick label (2009-June) and the origin. How can I get the graph to move over to make that more readable? I tried to put dummy data in, but then Matplotlib (correctly) treats that as data.

Upvotes: 2

Views: 2908

Answers (3)

cval
cval

Reputation: 6819

I suggest change Y axis limits:

ax.set_ylim([1.2, 1.8])

Upvotes: 1

Adam Cadien
Adam Cadien

Reputation: 1167

Because tick labels are text objects you can change their alignment. However to get access to the text properties you need to go through the set_yticklabels function. So add the line:

ax.set_yticklabels([1.4,1.6,1.8],va="bottom")

after your set_yticks call. Alternatively if you go through the pylab library directly, instead of accessing the function through the axes object, you can just set that in one line:

pylab.yticks([1.4,1.6,1.8],va="bottom")

Upvotes: 2

nye17
nye17

Reputation: 13357

add two limits to the x and y axes to shift the tick labels a bit.

# grow the y axis down by 0.05
ax.set_ylim(1.35, 1.8)
# expand the x axis by 0.5 at two ends
ax.set_xlim(-0.5, len(labels)-0.5)

the result is

enter image description here

Upvotes: 5

Related Questions