Search code examples
pythonmatplotlibaxis-labels

Matplotlib changing axis ticks


I have a few lines of making a scatter plot out of a collection of points (x0,y0), ... (xn,yn).

The truth is that I am only interested in the x scale as it is the scale on which the graph is power law behaved but after plotting I would like to replace the ticks on the x-axis by some other function of $x_i$.

How could I do so?


Solution

  • You can tell matplotlib where to put the x-axis labels by

    ax.set_xticks(positions)
    

    where positions is a list of tick positions in the scale which your data is using for x-positioning and ax is the axis to plot on (ax=plt.gca() or ax=fig.gca()). You can then set the tick labels:

    ax.set_xticklabels(labels)
    

    Example: you want to display x-ticks in log scale but still use a linear scale for the x-axis (not very useful, I know):

    positions = np.array([1, 10, 100, 1000])
    ax.set_xticks(positions)
    ax.set_xticklabels(np.log10(positions))
    

    Note however, that this won't change the x-scale of your data!