[英]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值... :(
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()
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.