matplotlib中使用极轴的四倍显示

2024-10-02 12:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在matplotlib中创建fourfold display

enter image description here

但无法得到极轴的逻辑。到目前为止,我一直在努力:

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]中。

  • 我不明白“宽度”是什么意思。


Tags: ofimportmatplotlibasdisplaynpbarplt
2条回答

万一有人对这里感兴趣,我就想到了

要使用本文中的Berkeley许可的例子,首先需要使用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

然后策划

^{pr2}$

使用

import numpy as np 
x1 = np.array([
            [1198, 1493], 
            [557, 1278]
            ])

x2 = ContTableIPFP(x1).flatten() 
FourfoldDisplay(x2)

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()

你会得到你期望的结果:

enter image description here

另一方面,对于您正在制作的精确绘图,在具有中心脊椎的矩形图上使用4Wedge可能更有意义。在

相关问题 更多 >

    热门问题