【问题标题】:Customise polar contour plot axes style自定义极轴等值线图轴样式
【发布时间】:2022-01-15 19:26:09
【问题描述】:

我正在使用 GridSpec 在 matplotlib 中创建一个极坐标等高线图网格。为了帮助视觉效果,我正在尝试自定义等高线图的外观。以这个单极图为例(MWE 下面)

我有两个格式问题:

  1. 如何自定义 r 轴以仅显示 0、20、40 和 60 个值?我怎样才能让这些数字也显示为白色?

  2. 在绘制多个极坐标图时,我选择使用ax.grid(False)ax.set_xticklabels([])ax.set_yticklabels([]) 删除另一个坐标轴和刻度。然而,沿 theta = 0 的白条仍然存在,我想将其删除。将此图像视为我的意思的示例:

MWE

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec

gs=gridspec.GridSpec(1,1)
gs.update(wspace=0.205, hspace=0.105) 
fig=plt.figure(figsize=(500/72.27,450/72.27))

X = np.arange(0, 70, 10)
Y = np.radians(np.linspace(0, 360, 20))
r, theta = np.meshgrid(X,Y)
Z1 = np.random.random((Y.size, X.size))


ax=fig.add_subplot(gs[0,0], projection='polar')
cax=ax.contourf(theta, r, Z1, 10)
plt.show()

【问题讨论】:

  • 您好,您能否也显示第二个示例的数据?
  • @MyWork,我注意到我plt.savefig 时没有显示白条,所以第二部分似乎不是错误。

标签: python matplotlib contour polar-coordinates contourf


【解决方案1】:

对于您的第一个问题,只需使用 matplotlib 中的 rticks(也可以查看 demo

ax.set_rticks([0,20,40,60])
ax.tick_params(colors='white', axis="y", which='both') 

完整代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec

gs=gridspec.GridSpec(1,1)
gs.update(wspace=0.205, hspace=0.105) 
fig=plt.figure(figsize=(500/72.27,450/72.27))

X = np.arange(0, 70, 10)
Y = np.radians(np.linspace(0, 360, 20))
r, theta = np.meshgrid(X,Y)
Z1 = np.random.random((Y.size, X.size))


ax=fig.add_subplot(gs[0,0], projection='polar')
cax=ax.contourf(theta, r, Z1, 10)

ax.set_rticks([0,20,40,60])
ax.tick_params(colors='white', axis="y", which='both') 
plt.show()

更新: 要使刻度具有不同的颜色,请使用我在评论中提到的内容:

colors = ['r', 'white', 'white', 'r']
for ytick, color in zip(ax.get_yticklabels(), colors):
    ytick.set_color(color)

来源:matplotlib different colors for each axis labelSet color for xticklabels individually in matplotlib

【讨论】:

猜你喜欢
  • 2012-11-14
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
相关资源
最近更新 更多