简体   繁体   中英

How to change ticks on a histogram? (matplotlib)

I have a histogram with only a few values. As a result, the x axis ticks are decimals:

在此输入图像描述

How can I make it 1,2,3,4,5?

You can use matplotlib.pyplot.xticks to set the locations of the x-axis tick marks.

Without the code used to generate the question's histogram, I resorted to creating data to produce a similar histogram. In this first example, we have a histogram with the default tick marks.

from pylab import hist, show

x = [1.1]*29 + [2]*7 + [3.2]*3 + [5]
hist(x)
show()

具有默认刻度线的直方图。

The objective is to have tick marks at 1, 2, 3, 4, and 5, and the next example does this by using xticks .

from pylab import hist, show, xticks

x = [1.1]*29 + [2]*7 + [3.2]*3 + [5]
hist(x)
xticks(range(1, 6))
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