我有一些示例代码可以绘制极坐标轮廓图:

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()


这给了我类似的东西:



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



...中间有空格,仅显示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(以获取下半球)。
这样产生:

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

10-12 17:35