如何自定义极坐标图中的扇区?保留指定区域突出数据
极坐标图保留指定扇区的实现方法
方法1:直接限制角度范围(最常用)
利用matplotlib极坐标轴的set_thetamin()和set_thetamax()方法,精准定义要保留的扇区角度范围,超出范围的区域会被自动隐藏。你可以根据红框标注的区域调整角度值,同时配合set_rmin()和set_rmax()限制径向范围,进一步突出目标数据:
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.scatter(np.deg2rad([75, 85, 95, 105]), [5.1, 5.2, 5.3, 5.4], s=20, c='navy', alpha=0.5) # 保留70°到110°的扇区(根据你的红框范围调整) ax.set_thetamin(70) ax.set_thetamax(110) # 限制径向范围,只显示5到6的区域,聚焦数据点 ax.set_rmin(5) ax.set_rmax(6) ax.grid(True) plt.show()
方法2:自定义刻度与网格,隐藏无关区域
如果需要更精细的控制,可以手动设置角度刻度只保留目标扇区的刻度,同时调整网格线的显示,弱化无关区域的存在感:
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.scatter(np.deg2rad([75, 85, 95, 105]), [5.1, 5.2, 5.3, 5.4], s=20, c='navy', alpha=0.5) # 设置角度范围 ax.set_thetamin(70) ax.set_thetamax(110) # 只显示目标扇区内的角度刻度 ax.set_xticks(np.deg2rad([70, 80, 90, 100, 110])) # 自定义径向刻度,聚焦数据所在区域 ax.set_yticks([5, 5.2, 5.4, 6]) ax.grid(True) plt.show()
方法3:调整角度起始与方向,让目标扇区居中
通过set_theta_offset()调整角度起始位置,让目标扇区位于图表视觉中心,提升信息的突出程度:
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.scatter(np.deg2rad([75, 85, 95, 105]), [5.1, 5.2, 5.3, 5.4], s=20, c='navy', alpha=0.5) # 设置角度偏移,让目标扇区中心(90°)位于图表顶部 ax.set_theta_offset(np.deg2rad(-90)) # 保留70°到110°的扇区 ax.set_thetamin(70) ax.set_thetamax(110) ax.set_rmax(6) ax.grid(True) plt.show()
内容的提问来源于stack exchange,提问作者ojipadeson
相关产品推荐
相关产品推荐

