在Matplotlib / Python中更改Polar Plots的轴选项

时间:2012-02-09 21:50:24

标签: python plot matplotlib axis-labels polar-coordinates

我在Matplotlib中更改轴标签时遇到问题。我想更改极坐标图中的径向轴选项。

基本上,我正在计算圆柱体的扭曲,这只是半径偏离原始(完全圆形)圆柱体的多少。一些失真值是负的,而一些是由于拉伸和压缩力而为正。我正在寻找一种以图形方式在圆柱坐标中表示这种情况的方法,所以我认为极地情节是我最好的选择。 Excel为我提供了一个“雷达图表”选项,该选项足够灵活,可以指定最小和最大径向轴值。我想使用Matplotlib在Python上复制它。

我在极坐标上绘图的Python脚本如下。

#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x    # in radians

offset = 2.0

R1 = [-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358]

fig1 = plt.figure()
ax1 = fig1.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax1.set_rmax(1)
ax1.plot(theta,R1,lw=2.5)

我的情节如下:bad

但这不是我想要呈现它的方式。我想改变我的径向轴,以便我可以将数据显示为偏离某个参考值,比如说-2。如何在极坐标中询问Matplotlib更改最小轴标签?我可以在Excel中轻松完成这项工作。我选择最小径向值-2,得到以下Excel雷达图:
excelplot

在Python上,我可以轻松地将输入数据偏移量为2.我的新数据集称为R2,如图所示:

#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x    # in radians

offset = 2.0

R2 = [1.642,1.517,1.521,1.654,1.879,2.137,2.358,2.483,2.479,2.346,2.121,1.863,\
1.642,1.517,1.521,1.654,1.879,2.137,2.358,2.483,2.479,2.346,2.121,1.863,1.642,\
1.517,1.521,1.654,1.879,2.137,2.358,2.483,2.479,2.346,2.121,1.863,1.642]

fig2 = plt.figure()
ax2 = fig2.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax2.plot(theta,R2,lw=2.5) 
ax2.set_rmax(1.5*offset)
plt.show()

情节如下所示:good

一旦我得到了这个,我就可以手动添加轴标签并将其硬编码到我的脚本中。但这是一种非常丑陋的方式。有没有什么方法可以直接获得相当于Excel雷达图表的Matplotlib并更改我的轴标签而无需操纵我的输入数据?

1 个答案:

答案 0 :(得分:7)

您可以使用设定轴限制的常规方法:

#!usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-180.0,190.0,10)
theta = (np.pi/180.0 )*x    # in radians

offset = 2.0

R1 = [-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358,-0.483,-0.479,-0.346,-0.121,0.137,0.358,0.483,0.479,0.346,0.121,\
-0.137,-0.358]

fig1 = plt.figure()
ax1 = fig1.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax1.set_ylim(-2,2)
ax1.set_yticks(np.arange(-2,2,0.5))
ax1.plot(theta,R1,lw=2.5)