📜  Python中的 matplotlib.pyplot.polar()(1)

📅  最后修改于: 2023-12-03 14:46:35.550000             🧑  作者: Mango

Python中的 matplotlib.pyplot.polar()

matplotlib.pyplot.polar()是Python中Matplotlib库中的一个极坐标绘图函数,它可以绘制极坐标图。本文将详细介绍matplotlib.pyplot.polar()的用法。

前置知识

在了解matplotlib.pyplot.polar()之前,需要掌握以下内容:

  1. 常见的极坐标系坐标的表达方式:(r, theta),其中r表示极坐标点到极点的距离,theta表示极角。
  2. Matplotlib库中的subplotsubplot2gridadd_axes三种字图方式的区别。
matplotlib.pyplot.polar()的基本语法

matplotlib.pyplot.polar(theta, r, **kwargs)

其中,thetar均为数组,分别表示极角和距离。

**kwargs用来设置绘图参数。常用的有colorlwls等,作用同Matplotlib库中常用参数。

matplotlib.pyplot.polar()的代码示例
import numpy as np
import matplotlib.pyplot as plt

theta = np.linspace(0, 2 * np.pi, 10, endpoint=False)
r = 6 * np.random.rand(len(theta))

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, polar=True)
ax.plot(theta, r, color='r', linewidth=3)
ax.set_ylim(0, 10)

plt.show()

以上代码用matplotlib.pyplot.polar()绘制了一个基本的极坐标图,函数的参数分别为thetar,其中theta为0到$2\pi$,由numpy.linspace()生成,r为10个0到6之间的随机数。此外,figsize设置绘图大小,subplot2grid()在绘图对象中创建一个子图,polar=True表示坐标系为极坐标系。这些在代码中都有体现。

matplotlib.pyplot.polar()的常用参数

matplotlib.pyplot.polar()的常用参数及说明如下:

| 参数 | 说明 | | --- | --- | | R | 极径数组 | | theta | 极角数组 | | color | 颜色 | | lw | 线宽 | | ls | 线型 | | marker | 标记 | | ms | 标记大小 |

使用示例:

import numpy as np
import matplotlib.pyplot as plt

theta = np.linspace(0, 2 * np.pi, 10, endpoint=False)
r = 6 * np.random.rand(len(theta))

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, polar=True)
ax.plot(theta, r, color='r', linewidth=3, ls='--', marker='*', ms=15)
ax.set_ylim(0, 10)

plt.show()

以上代码中,color参数设置为红色,lw参数设置为3,ls参数设置为虚线,marker参数设置为星号,ms参数设置为15。

参考文献
  1. Matplotlib Tutorial: https://matplotlib.org/stable/tutorials/index.html
  2. Matplotlib Examples: https://matplotlib.org/stable/gallery/index.html
总结

matplotlib.pyplot.polar()是Python中Matplotlib库中的极坐标绘图函数,可绘制极坐标图。使用时需了解常见的极坐标系坐标的表达方式以及Matplotlib库中的subplotsubplot2gridadd_axes三种字图方式的区别。在使用时可根据需要设置不同的参数,如colorlwls等。