gpt4 book ai didi

python - 如何在 matplotlib 中格式化极坐标等高线图

转载 作者:太空宇宙 更新时间:2023-11-04 05:48:13 25 4
gpt4 key购买 nike

我有一些示例代码可以制作极地等值线图:

import numpy as np
import matplotlib.pyplot as plt

azimuths = np.radians(np.linspace(0, 180, 90))
zeniths = np.arange(50, 5050, 50)

r, theta = np.meshgrid(zeniths, azimuths)
values = 90.0+5.0*np.random.random((len(azimuths), len(zeniths)))

fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.set_theta_zero_location("W")
pp = plt.contourf(theta, r, values, label='tmp')
cbar = plt.colorbar(pp, orientation='vertical')
cbar.ax.set_ylabel('scale label')
plt.show()

这给了我类似的东西:

enter image description here

...但我想要更像这样的东西:

enter image description here

...中间有空格,只显示 0 到 180 度。有谁知道执行此操作的便捷方法吗?

最佳答案

我不确定这有多方便,但这是一个可破解的解决方案(取自 here ):

import numpy as np

import mpl_toolkits.axisartist.floating_axes as floating_axes
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist.grid_finder import FixedLocator, MaxNLocator, \
DictFormatter
import matplotlib.pyplot as plt

tr = PolarAxes.PolarTransform()

degree_ticks = lambda d: (d*np.pi/180, "%d$^\\circ$"%(360-d))
angle_ticks = map(degree_ticks, np.linspace(180, 360, 5))
grid_locator1 = FixedLocator([v for v, s in angle_ticks])
tick_formatter1 = DictFormatter(dict(angle_ticks))
tick_formatter2 = DictFormatter(dict(zip(np.linspace(1000, 6000, 6),
map(str, np.linspace(0, 5000, 6)))))

grid_locator2 = MaxNLocator(5)

gh = floating_axes.GridHelperCurveLinear(tr,
extremes=(2*np.pi, np.pi, 1000, 6000),
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1,
tick_formatter2=tick_formatter2)

fig = plt.figure()
ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=gh)
fig.add_subplot(ax)

azimuths = np.radians(np.linspace(180, 360, 90)) # added 180 degrees
zeniths = np.arange(1050, 6050, 50) # added 1000

r, theta = np.meshgrid(zeniths, azimuths)
values = 90.0+5.0*np.random.random((len(azimuths), len(zeniths)))

aux_ax = ax.get_aux_axes(tr)
aux_ax.patch = ax.patch
ax.patch.zorder = 0.9

aux_ax.contourf(theta, r, values) # use aux_ax instead of ax

plt.show()

请注意(为了获得靠近原点的空间),您需要将所有数据点在半径方向上移动 1000,在 theta 方向上移动 pi(以获得下半球)。这产生:

enter image description here

关于python - 如何在 matplotlib 中格式化极坐标等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31323123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com