极坐标轴上的散点图

MatplotlibMatplotlibBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本教程中,我们将学习如何使用 Python 中的 Matplotlib 在极坐标轴上创建散点图。极坐标图是一种以极坐标显示数据的图形表示形式。当数据本质上是周期性或圆形的,例如随时间或方向测量的数据时,它非常有用。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们会立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("Scatter Plots") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/ModulesandPackagesGroup -.-> python/standard_libraries("Common Standard Libraries") python/PythonStandardLibraryGroup -.-> python/math_random("Math and Random") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48875{{"极坐标轴上的散点图"}} matplotlib/figures_axes -.-> lab-48875{{"极坐标轴上的散点图"}} matplotlib/scatter_plots -.-> lab-48875{{"极坐标轴上的散点图"}} python/tuples -.-> lab-48875{{"极坐标轴上的散点图"}} python/importing_modules -.-> lab-48875{{"极坐标轴上的散点图"}} python/standard_libraries -.-> lab-48875{{"极坐标轴上的散点图"}} python/math_random -.-> lab-48875{{"极坐标轴上的散点图"}} python/numerical_computing -.-> lab-48875{{"极坐标轴上的散点图"}} python/data_visualization -.-> lab-48875{{"极坐标轴上的散点图"}} end

导入必要的库

我们需要导入 Matplotlib 和 NumPy 库,以便在极坐标轴上创建散点图。我们还将设置随机种子以确保可重复性。

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

生成随机数据

我们将使用 NumPy 为散点图生成随机数据。我们将创建 150 个具有随机半径和角度值的数据点,并计算每个点的面积和颜色。

N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta

在极坐标轴上创建散点图

我们将使用 plt.scatter() 函数在极坐标轴上创建一个散点图。我们会将 projection 参数设置为 'polar',并将半径、角度、颜色和面积值作为参数传入。

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

创建一个原点偏移的极坐标散点图

我们可以通过设置 PolarAxes 对象的 set_rorigin()set_theta_zero_location() 方法,创建一个原点偏移的极坐标散点图。我们将把原点半径设置为 -2.5,并将 theta 零点位置设置为 'W',偏移量为 10

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

ax.set_rorigin(-2.5)
ax.set_theta_zero_location('W', offset=10)

创建一个限制在扇形区域内的极坐标散点图

我们可以通过设置 PolarAxes 对象的 set_thetamin()set_thetamax() 方法,在极坐标轴上创建一个限制在扇形区域内的散点图。我们将分别把 theta 的起始和结束限制设置为 45135

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

ax.set_thetamin(45)
ax.set_thetamax(135)

总结

在本教程中,我们学习了如何使用 Python 中的 Matplotlib 在极坐标轴上创建散点图。我们生成了随机数据,在极坐标轴上创建了散点图,创建了原点偏移的极坐标散点图,以及创建了限制在扇形区域内的极坐标散点图。极坐标图对于显示周期性或圆形数据非常有用,例如随时间或方向测量的数据。