出现错误值...使用 python matplotlib.ticker (ax.xaxis.set_major_locator)
Wrong values appears... using python matplotlib.ticker (ax.xaxis.set_major_locator)
我在使用 matplotlib.ticker
方法获取正确的 x-ticks 值时遇到了问题。这是描述我的问题的简单工作示例。
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]))
结果:x-ticks 位于正确的位置 (1,5,8) 但我想要的这些位置的值是 1,5,8(对应的 x 值)而不是 (1,2, 3)
我已经尝试了 https://matplotlib.org/examples/ticks_and_spines/tick-locators.html 中解释的所有定位器,但它们都显示 1,2,3 个 x-ticks 值...:(
我的实际问题(遇到相同问题的麻烦: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()
您已成功设置定位器。刻度线的位置确实在位置 1,5,8。
您缺少的是格式化程序。您希望在这些位置显示什么值?
您可以使用 FixedFormatter
,指定要显示的标签,
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter([1,5,8]))
您同样可以使用 ScalarFormatter
,它会根据位置自动选择刻度标签。
ax.xaxis.set_major_formatter(matplotlib.ticker.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])
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()
在这里使用 seaborn 点图可能不是最佳选择。通常的 matplotlib 图更有意义。对于这种情况,仅设置定位器也足够了,因为格式化程序已经自动设置为 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()
我在使用 matplotlib.ticker
方法获取正确的 x-ticks 值时遇到了问题。这是描述我的问题的简单工作示例。
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]))
结果:x-ticks 位于正确的位置 (1,5,8) 但我想要的这些位置的值是 1,5,8(对应的 x 值)而不是 (1,2, 3)
我已经尝试了 https://matplotlib.org/examples/ticks_and_spines/tick-locators.html 中解释的所有定位器,但它们都显示 1,2,3 个 x-ticks 值...:(
我的实际问题(遇到相同问题的麻烦: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()
您已成功设置定位器。刻度线的位置确实在位置 1,5,8。
您缺少的是格式化程序。您希望在这些位置显示什么值?
您可以使用 FixedFormatter
,指定要显示的标签,
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter([1,5,8]))
您同样可以使用 ScalarFormatter
,它会根据位置自动选择刻度标签。
ax.xaxis.set_major_formatter(matplotlib.ticker.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])
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()
在这里使用 seaborn 点图可能不是最佳选择。通常的 matplotlib 图更有意义。对于这种情况,仅设置定位器也足够了,因为格式化程序已经自动设置为 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()