Python matplotlib:设置主要和次要刻度强制相同的 x 和 y 比例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24963711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib: Setting both major and minor ticks forces same x and y scale
提问by owl
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.
这个问题与我之前问过的问题“ matplotlib:更改网格间隔并指定刻度标签”有关,但现在我想更改 x 和 y 轴的比例。当我设置 x 和 y 轴的范围,然后指定主要和次要刻度的间隔时,它会强制 x 和 y 轴相同。
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!!
我怎样才能获得 x 和 y 轴的不同范围,并且仍然有这样的主要和次要刻度?我可能遗漏了一些非常简单的东西,但如果有人能指出来,我非常感谢!!
采纳答案by BrenBarn
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.
那是因为您将 x 和 y 刻度设置为相同的值。如果您想要不同的尺寸,那么您还需要不同的刻度。您不能将两组刻度设置到相同的major_ticks
位置。为 x 轴制作一个刻度位置列表,为 y 轴制作一个单独的刻度位置列表,然后将每个轴的刻度设置为相应的列表。
回答by owl
I updated my code as below and now I get what I wanted! Thanks!
我更新了我的代码如下,现在我得到了我想要的!谢谢!
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)
# Set major ticks for x axis
major_xticks = np.arange(0, 501, 20)
# Set major ticks for y axis
major_yticks = np.arange(0, 301, 20)
# I want minor ticks for x axis
minor_xticks = np.arange(0, 501, 5)
# I want minor ticks for y axis
minor_yticks = np.arange(0, 301, 5)
# Specify tick label size
ax.tick_params(axis = 'both', which = 'major', labelsize = 6)
ax.tick_params(axis = 'both', which = 'minor', labelsize = 0)
# Suppress minor tick labels
ax.set_xticks(major_xticks)
ax.set_xticks(minor_yticks, minor = True)
ax.set_yticks(major_xticks)
ax.set_yticks(minor_yticks, 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()