设置主刻度和次刻度强制相同的 x 和 y 缩放
这个问题和我之前问的一个问题有关,那个问题是关于如何在matplotlib中改变网格间隔和指定刻度标签的。现在我想要改变x轴和y轴的刻度范围。当我设置了x轴和y轴的范围,并且指定了主要和次要刻度的间隔时,结果却把x轴和y轴的范围强制设成了一样。
这是我的代码。
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()
这是我得到的结果。
我该如何设置x轴和y轴不同的范围,同时又能像这样显示主要和次要刻度呢?我可能漏掉了一些很简单的东西,如果有人能指出来,我会非常感激!!
2 个回答
17
我更新了我的代码,下面是更新后的版本,现在我得到了我想要的结果!谢谢大家!
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()
5
这是因为你把x轴和y轴的刻度设置成了一样的。如果你想要不同的刻度大小,那就需要设置不同的刻度。你不能把两个轴的刻度都设置成相同的major_ticks
位置。你需要为x轴创建一份刻度位置的列表,然后为y轴再创建一份不同的刻度位置列表,最后把每个轴的刻度设置成对应的列表。