我已经在pyplot polar中绘制了一些数据进行分类。以下是图像之一
分类结果与预期不符。现在,我想尝试看看是否可以将值标记为高于和低于平均值,如以下预期结果所示。
这是我现在拥有的情节的代码...
theta = np.arange(0, 360 + 360 / 144, 360 / 144) * np.pi / 180
fig1 = plt.figure()
ax1 = fig1.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
ax1.set_ylim(0, 4500)
ax1.set_yticks(device_dict[name])
ax1.plot(theta, inter_data)
如何获得理想的结果?
颜色之间有一些空白。这是因为下一个数据点不满足条件,但是有什么办法可以解决?
这是一个例子。您首先需要计算平均值,然后使其成为要绘制的向量,然后使用该fill_between
函数对数据进行着色,如下所示。
import numpy as np
from matplotlib import pyplot as plt
theta = np.arange(0, 360 + 360 / 144, 360 / 144) * np.pi / 180
#Note that I have to make my own 'inter_data'
inter_data = (theta-np.pi/2.)*np.cos(theta)**2
#Calculate the mean, then multiply by vector of ones with same size as theta
inter_mean = np.nanmean(inter_data)*np.ones(len(theta))
fig1 = plt.figure(figsize=(8,8))
ax1 = fig1.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
ax1.plot(theta, inter_data,c='C0') #Plot the data
ax1.plot(theta, inter_mean,c='k') #Plot the mean
#Shade only the positive values where the data is greater than the mean red
ax1.fill_between(theta,inter_data,inter_mean,facecolor='red',alpha=.7,where=(inter_data-inter_mean)>0)
#Shade only the negative values where the data is less than the mean
ax1.fill_between(theta,inter_data,inter_mean,facecolor='yellow',alpha=.7,where=(inter_data-inter_mean)<0)
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句