我希望使用Python的Matplotlib添加一个楔子,概述一组极坐标数据。由于未知原因,我尝试使用楔形贴片艺术家,但没有成功。我希望了解这些未知,或者找到一个替代补丁艺术家的方法。在
主要的问题是楔块没有显示如我所期望的那样。考虑到我的代码,我希望它的方向是一个角度,半径范围为~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)
结果发现这比我一开始预想的要复杂得多。这里的主要问题是给
Wedge
的坐标和角度是轴坐标,而真正需要的是数据坐标中的一个楔子。尤其是角度有点难以调整。在我发现的解决方案是将楔形物的角点转换成轴坐标,然后使用这些点来计算楔形物的中心、半径和角度。也许有一种方法可以直接用数据坐标来实现,但至少这是可行的。我在matplotlib transformation tutorial和其他一些SO答案中找到了帮助:
为了使解决方案更易于解释,我在示例中更改了楔形坐标,并为计算中使用的几何点添加了一些编号注释。代码如下:
它所产生的形象:
解释:
在代码中,我定义了6个几何点:楔子的4个角和}线与楔体侧面之间的夹角(分别是点0,1,2,3和0,1,4,5)。这两个半径可以计算为楔形中心与点2和点3之间的欧几里德距离。有了这些数字,楔子最终可以被构造出来。在
theta=0
线上的两个点,它们对应于楔形物的内外半径。然后我使用转换ax.transData+ax.transAxes.inverted()
将这些点从数据转换为轴坐标。现在,在坐标轴上,我使用这些点来计算楔体的中心(楔体左右两侧的相交点;点2、3、4和5)以及{请注意,对于所有图形和轴操作,此解决方案并不可靠。特别是在添加楔子后改变轴极限或纵横比会使其错位。调整图形大小是可以的,并测试。希望这有帮助。在
旧答案:
这有点可笑,但显然半径参数与轴的数据无关。您可以通过添加一个半径为
^{pr2}$0.5
的楔形物来检查这一点,它与center=(0.5,0.5)
一起生成一个覆盖整个数据范围的楔形物。您可以定义一个函数来将楔形半径从数据坐标转换为这些坐标:这里}分别是轴的最小和最大半径。另一个问题是部分楔子是如何绘制的。根据文件:
rmin
和{所以在您的例子中,传递给
Wedge
的半径应该是外部的,而不是内部的。将它们放在一起,可以正确显示楔体:如果我误解了什么,请告诉我。在
相关问题 更多 >
编程相关推荐