我正在尝试在matplotlib中创建fourfold display:
但无法获得极轴的逻辑。这是我到目前为止所尝试的:
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]
。
我不明白“宽度”对应的是什么。
答案 0 :(得分:3)
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
可能更有意义。
答案 1 :(得分:1)
如果有人对此感兴趣,我会提出
要在本文中使用伯克利入场的例子,首先需要使用iterative proportional fitting来标准化值(以使等值边际)
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)