在极坐标轴中使用matplotlib实现四重显示
我正在尝试在matplotlib中创建一个四重显示:
但是我搞不懂极坐标轴的逻辑。这是我到目前为止尝试的:
import numpy as np
import matplotlib.pyplot as plt
# radius of each bar
radii = [10, 15, 20, 25]
# Value - width
width = np.pi/ 2
# angle of each bar
theta = [0,90,180,270]
ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width)
plt.show()
我不太确定我缺少了什么,但我只想要四个“相等”的区域,它们彼此接触。我无法搞定的是:
如何“控制”角度?我的意思是让所有四个“幻灯片”分别位于
[0,90], [90,180], [180, 270], [270, 360]
。我不明白“宽度”对应什么。
2 个回答
2
如果有人对此感兴趣,这里是我想到的内容
以论文中的伯克利大学录取为例,首先需要对数值进行标准化(也就是让它们的范围一致),可以使用迭代比例调整的方法。
def ContTableIPFP(x1ContTable):
''' poor man IPFP
compute iterative proportional fitting for
a 2 X 2 contingency table
Input :
a 2x2 contingency table as numpy array
Output :
numpy array with values standarized to equate margins
'''
import numpy as np
#Margins
xSumRows = np.sum(x1ContTable, axis = 0).tolist()
xSumCols = np.sum(x1ContTable, axis = 1).tolist()
# Seed
xq0 = x1ContTable/x1ContTable
# Iteration 1 : we adjust by row sums (i.e. using the sums of the columns)
xq1 = np.array([
(xq0[0] * xSumCols[0]).astype(float) / np.sum(xq0, axis = 0).tolist()[0],
(xq0[1] * xSumCols[1]).astype(float) / np.sum(xq0, axis = 0).tolist()[1],
]
)
#Iteration 2 : adjust by columns (i.e. using sums of rows)
xq2 = np.array([
(xq1[:,0] * xSumRows[0]).astype(float) / np.sum(xq1, axis = 0).tolist()[0],
(xq1[:,1] * xSumRows[1]).astype(float) / np.sum(xq1, axis = 0).tolist()[1],
]
)
return xq2.T
然后再进行绘图。
def FourfoldDisplay(radii):
''' radii = [10, 15, 20, 25]
'''
import numpy as np
import matplotlib.pyplot as plt
# Value - width
width = np.pi/ 2
# angle of each bar
theta = np.radians([0,90,180,270])
ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, alpha=0.5)
#labels
ax.set_xticklabels([])
ax.set_yticks([])
#plt.axis('off')
plt.show()
接下来使用
import numpy as np
x1 = np.array([
[1198, 1493],
[557, 1278]
])
x2 = ContTableIPFP(x1).flatten()
FourfoldDisplay(x2)
4
theta
应该用弧度表示,而不是度数。
如果你稍微调整一下你的代码:
import numpy as np
import matplotlib.pyplot as plt
# radius of each bar
radii = [10, 15, 20, 25]
# Value - width
width = np.pi/ 2
# angle of each bar
theta = np.radians([0,90,180,270])
ax = plt.subplot(111, polar=True)
bars = ax.bar(theta, radii, width=width, alpha=0.5)
plt.show()
你就会得到你想要的结果:
顺便提一下,对于你正在制作的这个图,使用4个 Wedge
在一个中心有坐标轴的矩形图上可能会更合适。