Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set my xlabel at the end of xaxis

I want my x axis has the label like this format

0 1 2 3 4 5 Xlabel 

but I try code below it result me in 2 lines

self.axes.set_xticks(np.arange(0,6,1)) self.axes.set_xlabel('Xlabel', fontsize=9,x=1,y=1) 

=> my result :(

0 1 2 3 4 5          Xlabel  
like image 453
billyduc Avatar asked Feb 15 '12 09:02

billyduc


People also ask

How do I change the position of Xlabel?

When setting the xlabel, the x parameter assigns the position in axis units, so 0 is the origin and 1 is the right edge of the plot. y is ignored as it's expected to be a default value, just below the tick marks. To override this behavior, you can set the position in axis units using the Axis set_label_coords method.

How do you set the Xlabel in Seaborn?

Use the set_xlabel() and set_ylabel() Functions to Set the Axis Labels in a Seaborn Plot. A seaborn plot returns a matplotlib axes instance type object. We can use the set_xlabel() and set_ylabel to set the x and y-axis label respectively. We can use the fontsize parameter to control the size of the font.

How do I change the Xlabel in Matplotlib?

To set labels on the x-axis and y-axis, use the plt. xlabel() and plt. ylabel() methods.

How do I change the position of a label in Matplotlib?

With matplotlib version 3.3. 0, the matplotlib functions set_xlabel and set_ylabel have a new parameter “loc” that can help adjust the positions of axis labels. For the x-axis label, it supports the values 'left', 'center', or 'right' to place the label towards left/center/right.


2 Answers

When setting the xlabel, the x parameter assigns the position in axis units, so 0 is the origin and 1 is the right edge of the plot. y is ignored as it's expected to be a default value, just below the tick marks.

To override this behavior, you can set the position in axis units using the Axis set_label_coords method. You can use other units by also providing a transform.

Here is an example of this:

import matplotlib.pyplot as plt import numpy as np  ax = plt.gca() ax.set_xticks(np.arange(0,6,1)) label = ax.set_xlabel('Xlabel', fontsize = 9) ax.xaxis.set_label_coords(1.05, -0.025)  plt.savefig('labelAtEnd.png') plt.show() 

Resulting in: enter image description here

The x value (1.05) was chosen to position the label outside the Axes frame. The y value (-0.025) was chose as a best guess to the position you desired. Using a transform, it might be possible to automatically position the text in line with the Tick labels.

EDIT:

Here's an extended example using a transform. It is not necissarily more helpful to use the last ticklabel's transform, because it does not take into account the size of the text and how it is aligned. So to get a somewhat desired effect, I had to 1) use the same font size for my x label, 2) position the vertical alignment (va) to 'top', and 3) position the horizontal alignment to 'left'. The transform for each tick is set for data units for x (because it's an xaxis) and axis units for y (0 to 1), but displaced by a fixed padding (in pixels) from the x axis.

import matplotlib.pyplot as plt import numpy as np  ax = plt.gca() ax.set_xticks(np.arange(0,6,1)) ax.set_yticks(np.arange(0,6,1)) label = ax.set_xlabel('xlabel', ha='left', va = 'top', )#fontsize = 9)  # need to draw the figure first to position the tick labels fig = plt.gcf() fig.draw(fig.canvas.get_renderer())  # get a tick and will position things next to the last one ticklab = ax.xaxis.get_ticklabels()[0] trans = ticklab.get_transform() ax.xaxis.set_label_coords(5.1, 0, transform=trans)  plt.savefig('labelAtEnd2.png') plt.show() 

This results in:

enter image description here

like image 185
Yann Avatar answered Oct 01 '22 04:10

Yann


In addition to what @Yann already said, it's actually easier to do this with annotate. It will also stay in the correct position when zoomed/panned.

import matplotlib.pyplot as plt import matplotlib as mpl  ticklabelpad = mpl.rcParams['xtick.major.pad']  fig, ax = plt.subplots() ax.set_xlim([0, 5])  # Add the label as annotation. The "5" is the padding betweent the right side # of the axis and the label... ax.annotate('XLabel', xy=(1,0), xytext=(5, -ticklabelpad), ha='left', va='top',             xycoords='axes fraction', textcoords='offset points')  plt.show() 

enter image description here

like image 35
Joe Kington Avatar answered Oct 01 '22 04:10

Joe Kington