13

My axis labels often look not good (too close to tick labels) when I use Matplotlib.

test image

How to set distance between tick labels and axis label? I just need to enlarge distance between "Number of stars" label and corresponding tick labels. Maybe latex \vspace{} is working but I don't know how to implement it. fig.subplots_adjust(left=) is not a solution.

kmario23
  • 42,075
  • 12
  • 123
  • 130
drastega
  • 1,319
  • 3
  • 26
  • 39
  • 1
    If I understand you correctly, you are looking for the `labelpad` parameter in the [`ax.set_ylabel()`](http://matplotlib.org/api/axes_api.html?highlight=label#matplotlib.axes.Axes.set_ylabel) method. This takes a number of points to be used as spacing between the axis and the corresponding label. – sodd Feb 05 '14 at 08:40
  • Thank you!!! Write this as answer. – drastega Feb 05 '14 at 10:32
  • Do you know how to do the same for 'drawparallels' in Basemap (Matplotlib)? – drastega Feb 05 '14 at 12:34

2 Answers2

18

To move the axis label further away from the axis, you can include an argument to the optional labelpad parameter of the corresponding method used to set the label, i.e. ax.set_ylabel() or ax.set_xlabel(). This parameter takes a scalar which is the spacing between the axis and the label given in points.

As to achieve the same effect when using the drawparallels() method of Basemap, I believe you can include an argument to the parameters xoffset or yoffset, depending on which axis you want to alter the placement of the text. The arguments of these parameters have "units" of map width, meaning e.g. xoffset=0.05 makes the offset of the x-ticks 5 % of the map width.

sodd
  • 10,634
  • 3
  • 47
  • 59
  • Rolled back to my original answer, see [this post](http://meta.stackexchange.com/a/120583/223330) for more info as to why. – sodd Aug 29 '14 at 18:59
  • 2
    while this answer was satisfactory for the questioner, it is not entirely right: labelpad does *not* set the distance between the axis and the axis label, it sets the distance between the tick labels and the axis label. – Bastian May 24 '19 at 14:00
4

This is a really subjective question, but I'll take a stab anyway:

The figsize kwarg takes the width and height of the figure in inches.

A4 paper is 8.3" by 11.7". So let's say, for the sake of argument, you want 1" margins.

paperheight = 11.7
paperwidth = 8.3
margin = 1.0

fig = plt.figure(figsize=(paperwidth - 2*margin, paperheight - 2*margin))

# plotting stuff

fig.tight_layout()
fig.savefig(...)

The call to tight_layout() will give the labels a bit more room and make sure that everything is expanded out to the figure edges as much as practicable.

Paul H
  • 52,530
  • 16
  • 137
  • 125
  • Sorry, I have changed my question. – drastega Feb 03 '14 at 22:54
  • @user2579566 I see that. It doesn't change my answer however. Simply change `paperheight` and `paperwidth` as needed and change `margin` as desired – Paul H Feb 03 '14 at 22:55
  • @user2579566 can you be more specific? works on my end. – Paul H Feb 03 '14 at 23:12
  • My axes labels overlap figures when a I use your values (11.7,8.3,1.0) – drastega Feb 03 '14 at 23:15
  • @user2579566 It's going to be impossible to diagnose until you show the code you're using. Are you calling `fig.tight_layout()?` – Paul H Feb 04 '14 at 00:19
  • Yes I do. I just need to enlarge distance between left "Number of stars" labels and corresponding pictures. I use usual fig.add_subplot() and plot() functions. – drastega Feb 04 '14 at 00:40
  • @user2579566 I simply cannot make any further suggestions until you provide a short, self-contained example that demonstrates the problem you're seeing. I started it for you in my response. All you need to do is fill in your part where it says `# plotting stuff` (http://sscce.org/) – Paul H Feb 04 '14 at 00:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46711/discussion-between-paul-h-and-user2579566) – Paul H Feb 04 '14 at 00:50