简体   繁体   中英

How to change spacing between ticks in matplotlib?

I want to plot a graph with a lot of ticks on the X axis using the following code:

import pylab

N = 100
data = pylab.np.linspace(0, N, N)

pylab.plot(data)

pylab.xticks(range(N)) # add loads of ticks
pylab.grid()
pylab.tight_layout()
pylab.show()

pylab.close()

The spacing between ticklabels is exclusively determined by the space between ticks on the axes. Therefore the only way to obtain more space between given ticklabels is to make the axes larger.

In order to determine the space needed for the labels not to overlap, one may find out the largest label and multiply its length by the number of ticklabels. One may then adapt the margin around the axes and set the calculated size as a new figure size.

import numpy as np
import matplotlib.pyplot as plt

N = 150
data = np.linspace(0, N, N)

plt.plot(data)

plt.xticks(range(N)) # add loads of ticks
plt.grid()

plt.gca().margins(x=0)
plt.gcf().canvas.draw()
tl = plt.gca().get_xticklabels()
maxsize = max([t.get_window_extent().width for t in tl])
m = 0.2 # inch margin
s = maxsize/plt.gcf().dpi*N+2*m
margin = m/plt.gcf().get_size_inches()[0]

plt.gcf().subplots_adjust(left=margin, right=1.-margin)
plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])

plt.savefig(__file__+".png")
plt.show()

在此处输入图片说明

Note that if the figure shown in the plotting window is larger than the screen, it will be shrunk again, so the resized figure is only shown in its new size when saved. Or, one may choose to incorporate it in some window with scrollbars as shown in this question: Scrollbar on Matplotlib showing page

您可以使用以下方法旋转标签并使字体变小:

ax.set_xticklabels(rotation = (45), fontsize = 10, va='bottom', ha='left')

This is an old question, but for anyone searching, there is a way: ticker.MaxNLocator (and the other locators there). It can put a maximum of N ticks on a graph, no matter the zoom size.

One other solution is to adjust your data to make the tick spacing work.

Note: if you have huge amounts of data this may become very difficult

import matplotlib.pyplot as plt
import numpy as np

# **************************************************************************
# This plot shows inconsistent spacing between ticks
# **************************************************************************
f, test = plt.subplots()
xdata = [0,1,5,6,9,10]
ydata = [1,2,3,4,5,6]
test.step(xdata,ydata)
test.set_xticks([0,1,5,6,9,10],['Zero', 'one', 'five', 'six', 'nine', 'ten'])

plt.show()

# **************************************************************************
# Adjust data so tick marks have consistent spacing
# **************************************************************************
prettyxdata = xdata
for i in range(len(xdata)):
    if prettyxdata [i] == 5:
        prettyxdata [i]=2
    elif prettyxdata [i] == 6:
        prettyxdata [i]=3
    elif prettyxdata [i] == 9:
        prettyxdata [i]=4
    elif prettyxdata [i] == 10:
        prettyxdata [i]=5

f, test = plt.subplots()
test.step(prettyxdata,ydata)
test.set_xticks([0,1,2,3,4,5],['Zero', 'one', 'five', 'six', 'nine', 'ten'])

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM