简体   繁体   中英

Wrong values appears… using python matplotlib.ticker (ax.xaxis.set_major_locator)

I am having trouble to get the right x-ticks values by using matplotlib.ticker methods. Here is the simple working example to describe my problem.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

## Sample code
np.arange(0, 15, 5)
plt.figure(figsize = [6,4])

x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
y=np.array([15,16,17,18,19,20,40,50,60,70,80,90,100,110,120])
ax = sns.pointplot(x,y, color='k', markers=["."], scale = 2)  
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([1,5,8]))

Results: x-ticks is located at the right position(1,5,8) but the values that I want for those positions are 1,5,8 (corresponding x values) instead of (1,2,3)

I have tried all locators explained in https://matplotlib.org/examples/ticks_and_spines/tick-locators.html , but it all shows 1,2,3 x-ticks value... :(

wrongtickvalue

My actual problem (Having trouble of identical issues: x-ticks should be something like 64, 273.5, 1152.5 instead of the first three numbers)

...
print(intervals}
>> [64, 74, 86.5, 10.1, 116.0, 132.0, 152.0, 175.5, 204.0, 236.0, 273.5, 319.0, 371.0, 434.0, 509.0, 595.5, 701.0, 861.0, 1152.5]

ax.xaxis.set_major_locator(matplotlib.ticker.LinearLocator(3)
plt.show()

wrong_tick_value2

You have successfully set the locator. The locations of the ticks are indeed at postions 1,5,8.

What you are missing is the formatter. What values do you want to show at those locations?
You may use a FixedFormatter , specifying the labels to show,

ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter([1,5,8]))

You could equally use a ScalarFormatter , which would automatically choose the ticklabels according to their positions.

ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())

Complete code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns

## Sample code
np.arange(0, 15, 5)
plt.figure(figsize = [6,4])

x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
y=np.array([15,16,17,18,19,20,40,50,60,70,80,90,100,110,120])
ax = sns.pointplot(x,y, color='k', markers=["."], scale = 2)  
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([1,5,8]))
ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
# or use
#ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter([1,5,8]))

plt.show()

在此处输入图片说明


Using a seaborn pointplot may not be the best choice here. A usual matplotlib plot makes much more sense. For that case it would also be sufficient to only set the locator, because the formatter is already set to a ScalarFormatter automatically.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns

## Sample code
np.arange(0, 15, 5)
plt.figure(figsize = [6,4])

x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
y=np.array([15,16,17,18,19,20,40,50,60,70,80,90,100,110,120])
plt.plot(x,y, marker="o") 
plt.gca().xaxis.set_major_locator(matplotlib.ticker.FixedLocator([1,5,8]))

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