简体   繁体   中英

How to set matplotlib axis major ticks with a list of values?

I have some data I want to plot on a log scale, with a specific set of major y-axis ticks

I know I can use something like

plt.scatter(x, y)
ax = plt.gca()
ax.yaxis.set_major_locator(ticker.MultipleLocator(100))  

To specify ticks, but I don't know how to pass an instance of matplotlib.ticker.Locator with arbitrary values eg if I want to set the ticks to [2,4,100] instead of fixed increments. How do I turn an array of tick values to an object that set_major_locator will accept?

Any reason you don't want to just use the call to yticks ?

x = [1,2,3,4,5]
y = [2,10,40,50,100]

tick_array = [2, 4, 100] # an array of tick values
plt.scatter(x, y, s=150)
plt.yticks(ticks=tick_array)
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