【发布时间】:2019-06-21 01:27:54
【问题描述】:
我希望使用 Python 的 Matplotlib 添加一个楔形,勾勒出一组极坐标数据。由于未知原因,我尝试使用 Wedge 补丁艺术家未成功。我希望了解这些未知数,或者找到补丁艺术家方法的替代方法。
主要问题是 Wedge 补丁未按预期显示。给定我的代码,我希望它以一个角度定向,并跨越约 0.05 的半径范围。这将其置于此处的扇区图中: 1
但是这个楔子的尺寸和位置与我预期的不同。在查看缩小的图时,它也会发生变化: 2
楔形的角度范围大致正确(大约 ~25-27 度),但它从错误的半径开始(应该是 ~0.4),并且是错误的宽度(应该是 ~0.05)。为什么会这样?如何绘制具有这些所需尺寸的楔形?
我已经查看并改编了类似问题的代码(例如,参见Python: Add a Ring Sector or a Wedge to a Polar Plot)。
这是我的主要代码的改编,包括示例数据。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
##Enter data
thetaRad = np.array([0.455, 0.456, 0.455, 0.456, 0.46 , 0.459, 0.461, 0.461, 0.453,
0.459, 0.46 , 0.46 , 0.46 , 0.451, 0.46 , 0.457, 0.45 , 0.451,
0.45 , 0.45 , 0.451, 0.452, 0.461, 0.459, 0.451, 0.455, 0.454,
0.457, 0.459, 0.451, 0.46 , 0.453, 0.46 , 0.452, 0.452, 0.45 ,
0.453, 0.452, 0.452, 0.456, 0.45 , 0.458, 0.461, 0.457, 0.45 ,
0.453, 0.459, 0.459, 0.455, 0.456, 0.457, 0.457, 0.454, 0.453,
0.455, 0.456, 0.459, 0.455, 0.453, 0.455, 0.454, 0.459, 0.457,
0.454, 0.46 , 0.458, 0.459, 0.457, 0.451, 0.45 , 0.455, 0.461,
0.455, 0.458, 0.456, 0.449, 0.459, 0.453, 0.458, 0.457, 0.456,
0.45 , 0.459, 0.458, 0.453, 0.452, 0.459, 0.454, 0.455, 0.452,
0.453, 0.451, 0.453, 0.461, 0.452, 0.458, 0.449, 0.461, 0.459,
0.452, 0.458, 0.455, 0.452, 0.451, 0.457, 0.457, 0.457, 0.457,
0.456, 0.456, 0.451, 0.451, 0.452, 0.459, 0.45 , 0.453, 0.45 ,
0.449, 0.453, 0.455, 0.457])
Zs = np.array([0.052, 0.052, 0.057, 0.058, 0.058, 0.058, 0.058, 0.058, 0.059,
0.059, 0.059, 0.059, 0.06 , 0.06 , 0.06 , 0.06 , 0.064, 0.134,
0.134, 0.134, 0.134, 0.135, 0.135, 0.135, 0.135, 0.135, 0.135,
0.135, 0.135, 0.135, 0.135, 0.135, 0.135, 0.136, 0.136, 0.136,
0.136, 0.136, 0.136, 0.137, 0.309, 0.311, 0.32 , 0.328, 0.352,
0.379, 0.381, 0.381, 0.382, 0.382, 0.383, 0.383, 0.386, 0.387,
0.39 , 0.392, 0.392, 0.392, 0.392, 0.393, 0.393, 0.394, 0.394,
0.394, 0.394, 0.394, 0.394, 0.395, 0.395, 0.396, 0.422, 0.426,
0.48 , 0.482, 0.483, 0.483, 0.484, 0.487, 0.487, 0.489, 0.489,
0.49 , 0.49 , 0.491, 0.491, 0.491, 0.491, 0.492, 0.492, 0.496,
0.497, 0.498, 0.5 , 0.505, 0.764, 0.767, 0.771, 0.771, 0.777,
0.833, 0.844, 0.855, 0.858, 0.863, 0.866, 0.868, 0.869, 0.87 ,
0.871, 0.872, 0.875, 0.994, 0.995, 0.996, 1.002, 1.004, 1.01 ,
1.01 , 1.011, 1.475, 1.667])
maxZ = 0.55
minZ = 0.28
##Prepare plot
fig = plt.figure()
color = 'k'
m = 'o'
size = 1
ax = fig.add_subplot(111, projection='polar')
plt.scatter(thetaRad,Zs, c=color, marker=m, s = size)
ax.set_rmax(maxZ)
ax.set_rmin(minZ)
#set theta limits to be scaled from the dataset
minTheta = 0.95*min(thetaRad)
maxTheta = 1.05*max(thetaRad)
#uncomment these for the partial sector plot:
#ax.set_thetamin(np.rad2deg(minTheta))
#ax.set_thetamax(np.rad2deg(maxTheta))
#ax.set_rorigin(-minZ)
ticks = np.linspace(minTheta, maxTheta, 4)
ax.set_xticks(ticks)
##Add a wedge
#define the wedge's width and range
window = np.array([0.35,0.40])
dTheta = np.deg2rad(0.5)
wedgeRange = [minTheta+dTheta, maxTheta-dTheta]
wedgeRange = np.rad2deg(wedgeRange)
r = window[1]
width = window[1]-window[0]
width = width
#apparently, plt's polar plot is silently centered at (0.5,0.5) instead of the
#origin, so set this:
center = (0.5,0.5)
wedge = Wedge(center, r, wedgeRange[0],wedgeRange[1],width=width, transform=ax.transAxes, linestyle='--', fill=False, color='red')
ax.add_artist(wedge)
【问题讨论】:
标签: python matplotlib plot data-visualization polar-coordinates