matplotlib:设置主要和次要蜱都强制使用相同的x和y比例 [英] matplotlib: Setting both major and minor ticks forces same x and y scale
问题描述
这个问题与我先前问的问题有关: matplotlib:更改网格间隔并指定刻度线标签但现在我想更改x和y轴的比例。当我设置x轴和y轴的范围,然后指定主要和次要蜱的间隔时,它强制x和y轴相同。
这是我的代码。
import matplotlib.pyplot as plt
import numpy as np
fig = bt.figure()
ax = fig.add_subplot(111)
for key,sort(data.items())中的值:
x = value [0] [ 2]
y = value [0] [3]
count = value [0] [4]
ax.annotate(count,xy =(x,y),size =')
plt.suptitle('Count of count',fontsize = 12)
ax.set_xlabel('x')
ax.set_ylabel(' y')
ax.set_aspect('equal')
#我希望max x轴为500
ax.set_xlim(0,501)
#I想要最大轴为300
ax.set_ylim(0,301)
#我希望主要滴答是每20
major_ticks = np.arange(0,501, 20)
#我希望每个5美元的小勾号b $ b minor_ticks = np.arange(0,501,5)
#如果我做minor_ticks = np.arange (0,301,5),我不会得到整个阴谋的小勾号
#指定勾号标签大小
ax.tick_params(axis ='both',which ='major' ,labelsize = 4)
ax.tick_params(axis ='both',which ='minor',labelsize = 0)
#抑制次标签标签
ax.set_xticks(
ax.set_xticks(minor_ticks,minor = True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks,minor = True)
#Set两个蜱都在
之外ax.tick_params(which ='both',direction ='out')
#指定主要和次要网格的不同设置
ax.grid( which ='minor',alpha = 0.3)
ax.grid(which ='major',alpha = 0.7)
filename ='C:\ Users \Owl\Desktop \Plot.png'
plt.savefig(文件名,dpi = 150)
plt.close()
这就是我得到的结果。
如何我是否可以为x和y轴获得不同的范围,并仍然有像这样的主要和次要的蜱虫?我可能会错过一些非常简单的东西,但如果有人能指出,我非常感谢你!
您将x和y的刻度设置为相同的东西。如果你想要不同的尺寸,那么你还需要不同的刻度。您不能将两组报价设置为相同的 major_ticks
头寸。为x轴创建一个勾选位置列表,并为y轴创建一个单独的勾选位置列表,然后将每个轴的勾号设置为相应的列表。 This question is related to the earlier question I asked "matplotlib: Change grid interval and specify tick labels" but now I want to change the scale for x and y axes. When I set the range for x and y axes and then specify the intervals for major and minor ticks, it forces the x and y axes to be the same.
This is my code.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
for key, value in sorted(data.items()):
x = value[0][2]
y = value[0][3]
count = value[0][4]
ax.annotate(count, xy = (x, y), size = 3)
plt.suptitle('Number of counts', fontsize = 12)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_aspect('equal')
# I want max x axis to be 500
ax.set_xlim(0, 501)
# I want max y axis to be 300
ax.set_ylim(0, 301)
# I want major ticks to be every 20
major_ticks = np.arange(0, 501, 20)
# I want minor ticks to be every 5
minor_ticks = np.arange(0, 501, 5)
# If I do minor_ticks = np.arange(0, 301, 5), I will not get minor ticks for the entire plot
# Specify tick label size
ax.tick_params(axis = 'both', which = 'major', labelsize = 4)
ax.tick_params(axis = 'both', which = 'minor', labelsize = 0)
# Suppress minor tick labels
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor = True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor = True)
# Set both ticks to be outside
ax.tick_params(which = 'both', direction = 'out')
# Specify different settings for major and minor grids
ax.grid(which = 'minor', alpha = 0.3)
ax.grid(which = 'major', alpha = 0.7)
filename = 'C:\Users\Owl\Desktop\Plot.png'
plt.savefig(filename, dpi = 150)
plt.close()
and this is what I get.
How could I get different ranges for x and y axes and still have major and minor ticks like this? I might be missing something very simple but if somebody could point it out, I am very thankful for that!!
That's because you set the x and y ticks to the same thing. If you want different sizes, then you also need different ticks. You can't set both sets of ticks to the same major_ticks
positions. Make one list of tick positions for the x axis and a separate list of tick positions for y axis and then set the ticks for each axis to the corresponding list.
这篇关于matplotlib:设置主要和次要蜱都强制使用相同的x和y比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!