轴不显示我想要的记号

2024-04-23 07:05:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一个拉玛钱德隆的阴谋。在这种图上,x从-180°到180°,y也是,我希望每隔60度打一个勾。下面是我使用的代码:

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

x = [-179, 179]
y = [-179, 179]

fig = plt.figure(1)
ax = plt.subplot(111)

ax.axis([-180, 180, -180, 180])

ax.set_xticks([-180, -120, -60, 0, 60, 120, 180])
ax.set_yticks([-180, -120, -60, 0, 60, 120, 180])


# 1 bim = 1 degree
# !!! Logarithmic normalization of the colors
plt.hist2d(x, y, bins=180, norm=LogNorm())
plt.colorbar()
plt.show()

在这个例子中,我只画了两点。但没有显示记号-180和180,因为没有轴:

enter image description here

如果我把x和y改为:

^{pr2}$

我得到了我想要的:

enter image description here

有没有办法在不改变数据的情况下达到第二个结果?


Tags: 代码fromimportmatplotlibasfigpltax
1条回答
网友
1楼 · 发布于 2024-04-23 07:05:40

使用hist2d,打印后设置轴刻度:

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

x = [-179, 179]
y = [-179, 179]

fig = plt.figure(1)
ax = plt.subplot(111)

# 1 bim = 1 degree
# !!! Logarithmic normalization of the colors
plt.hist2d(x, y, bins=180, norm=LogNorm())
plt.colorbar()

ax.axis([-180, 180, -180, 180])
ax.set_xticks([-180, -120, -60, 0, 60, 120, 180])
ax.set_yticks([-180, -120, -60, 0, 60, 120, 180])

plt.show()

enter image description here

相关问题 更多 >