在Matplotlib/Python中更改极坐标图的轴选项 [英] Changing axis options for Polar Plots in Matplotlib/Python

查看:679
本文介绍了在Matplotlib/Python中更改极坐标图的轴选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem changing my axis labels in Matplotlib. I want to change the radial axis options in my Polar Plot.

基本上,我正在计算圆柱体的变形,它不过是半径与原始(完全为圆形)圆柱体的偏离量而已.由于拉伸力和压缩力,一些变形值是负的,而一些变形值是正的.我正在寻找一种以图形方式在圆柱坐标中表示这一点的方法,因此我认为极坐标图是我最好的选择. Excel为我提供了一个雷达图"选项,该选项足够灵活,可以让我指定最小和最大径向轴值.我想使用Matplotlib在Python上复制它.

Basically, I'm computing the distortion of a cylinder, which is nothing but how much the radius deviates from the original (perfectly circular) cylinder. Some of the distortion values are negative, while some are positive due to tensile and compressive forces. I'm looking for a way to represent this in cylindrical coordinates graphically, so I thought that a polar plot was my best bet. Excel gives me a 'radar chart' option which is flexible enough to let me specify minimum and maximum radial axis values. I want to replicate this on Python using Matplotlib.

我的用于绘制极坐标的Python脚本如下.

My Python script for plotting on polar coordinates is as follows.

#!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)

我的情节如下:

但这不是我要呈现的方式.我想改变径向轴,以便将数据显示为与某些参考值(例如-2)的偏差.我如何要求极坐标中的Matplotlib更改最小轴标签?我可以在Excel中非常轻松地完成此操作.我选择最小径向值-2,以获得以下Excel雷达图:

But this is not how I want to present it. I want to vary my radial axis, so that I can show the data as a deviation from some reference value, say -2. How do I ask Matplotlib in polar coordinates to change the minimum axis label? I can do this VERY easily in Excel. I choose a minimum radial value of -2, to get the following Excel radar chart:

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

On Python, I can easily offset my input data by a magnitude of 2. My new dataset is called R2, as shown:

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

该图显示如下:

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

Once I get this, I can MANUALLY add axis labels and hard-code it into my script. But this is a really ugly way. Is there any way I can directly get a Matplotlib equivalent of the Excel radar chart and change my axis labels without having to manipulate my input data?

推荐答案

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

You can just use the normal way of setting axis limits:

#!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)

这篇关于在Matplotlib/Python中更改极坐标图的轴选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆