简体   繁体   English

出现错误的值…使用python matplotlib.ticker(ax.xaxis.set_major_locator)

[英]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. 我很难通过使用matplotlib.ticker方法来获取正确的x-ticks值。 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) 结果:x-点位于正确的位置(1,5,8),但我想要的那些位置的值是1,5,8(对应的x值),而不是(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... :( 我已经尝试过https://matplotlib.org/examples/ticks_and_spines/tick-locators.html中解释的所有定位符,但是都显示了1,2,3个x-ticks值... :(

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) 我的实际问题(遇到相同问题的麻烦:x-ticks应该类似于64、273.5、1152.5,而不是前三个数字)

...
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. s的位置确实在位置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, 您可以使用FixedFormatter ,指定要显示的标签,

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. 您同样可以使用ScalarFormatter ,它会根据它们的位置自动选择刻度标签。

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. 常见的matplotlib图更有意义。 For that case it would also be sufficient to only set the locator, because the formatter is already set to a ScalarFormatter automatically. 在这种情况下,仅设置定位器就足够了,因为格式化程序已经自动设置为ScalarFormatter。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 中的 matplotlib.ticker 对 yaxis 进行舍入 - how to round the yaxis using matplotlib.ticker in python python matplotlib - 检索 xaxis_major_locator 作为时间值 - python matplotlib - retrieve xaxis_major_locator as time value 使用matplotlib.ticker设置Matplotlib Ytick标签会产生关键错误 - Setting Matplotlib Ytick labels with matplotlib.ticker yields key error Matplotlib:将图,xaxis,minor_locator,major_locator放在一起 - Matplotlib: putting together figure, xaxis, minor_locator, major_locator Python Matplotlib (1) 将 x 轴标签格式化为 Year-Quarter 和 (2) 将 major_locator 设置为月末 - Python Matplotlib (1) format x-axis labels to Year-Quarter and (2) set major_locator to end of month matplotlib-我应该使用哪种set_major_locator - matplotlib - what kind of set_major_locator should I use MatplotLib 3.0.1中ax.set_xlabel()和ax.xaxis.set_label()之间的区别 - Difference between ax.set_xlabel() and ax.xaxis.set_label() in MatplotLib 3.0.1 使用matplotlib设置置顶器电源限制的颜色 - Set the color of the ticker powerlimits using matplotlib 使用matplotlib.axis.XAxis.set_ticks_position('bottom')时发生Python Matplotlib错误 - Python Matplotlib error when using matplotlib.axis.XAxis.set_ticks_position('bottom') Matplotlib使用ax.set()旋转xticklabel - Matplotlib Rotate xticklabels using ax.set()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM