ZetCode

Matplotlib 极坐标图

最后修改于 2025 年 3 月 7 日

Matplotlib 是一个通用的 Python 库,用于创建各种各样的可视化,包括极坐标图。 极坐标图,也称为径向图或圆形图,在圆形坐标系中绘制数据,使其非常适合显示循环或方向性数据。 本教程探讨如何使用 Matplotlib 创建各种类型的极坐标图。

极坐标图特别适用于可视化周期性现象,例如风向、日常活动模式或季节性趋势。 Matplotlib 的极坐标绘图功能允许灵活的自定义,使用户能够以直观且视觉上吸引人的方式表示数据。

基本极坐标图

此示例演示如何创建一个基本的极坐标图,显示按方向划分的风速。

basic_polar_chart.py
import matplotlib.pyplot as plt
import numpy as np

# Data: Directions (degrees) and wind speed (m/s)
theta = np.radians([0, 90, 180, 270, 360])  # N, E, S, W, N
wind_speed = [5, 8, 6, 7, 5]

# Create a polar chart
ax = plt.subplot(111, projection="polar")
ax.plot(theta, wind_speed, color="blue", label="Wind Speed")
ax.fill_between(theta, 0, wind_speed, color="skyblue", alpha=0.4)

# Customize the chart
ax.set_theta_zero_location("N")  # North at top
ax.set_theta_direction(-1)  # Clockwise
ax.set_title("Wind Speed by Direction (March 2025)")
ax.set_xlabel("Direction")
ax.legend()

# Display the chart
plt.show()

在此示例中,我们绘制了 2025 年 3 月某一天各个主要方向(北、东、南、西)的风速。 np.radians 函数将角度转换为弧度,因为 Matplotlib 的极坐标图使用弧度。 带有 projection="polar"subplot 函数设置圆形坐标系。 fill_between 方法填充从中心 (0) 到风速值的区域,从而创建径向效果。

该图通过 set_theta_zero_location("N") 定制,以将北方对齐在顶部,并通过 set_theta_direction(-1) 进行顺时针旋转,模仿指南针。 风速在东方达到峰值 8 米/秒,提供了方向变化的清晰视觉效果——对于研究风向的气象学家或可再生能源分析师很有用。

极坐标条形图

此示例显示如何创建极坐标条形图以显示每日活动时间。

polar_bar_chart.py
import matplotlib.pyplot as plt
import numpy as np

# Data: Hours of the day and activity duration (hours)
theta = np.linspace(0, 2 * np.pi, 24, endpoint=False)  # 24 hours
activity = [1, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 2, 3, 4, 5, 4, 3, 2, 1, 1]

# Create a polar bar chart
ax = plt.subplot(111, projection="polar")
width = 2 * np.pi / 24  # Width of each bar
bars = ax.bar(theta, activity, width=width, color="skyblue", alpha=0.7)

# Customize the chart
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_title("Daily Activity Hours (March 7, 2025)")
ax.set_xlabel("Time of Day")

# Display the chart
plt.show()

此极坐标条形图表示某人在 2025 年 3 月 7 日的 24 小时内的活动持续时间。 np.linspace 函数将圆分成 24 个相等的段(每小时一个),ax.bar 创建从中心辐射出的条,高度对应于活动时间。 width 参数确保条跨越一小时,alpha=0.7 增加轻微的透明度。

活动在上午 9 点到下午 5 点之间达到高峰(最多 6 小时),可能反映了工作或锻炼,并在晚上逐渐减少。 极坐标布局自然地传达了一天的周期性,使其成为基于时间的数据(如睡眠模式、能源使用或交通流量)的理想选择。 该图的指南针式方向增强了其直观性。

具有多个数据集的极坐标面积图

此示例演示如何创建一个极坐标面积图,用于比较各季节的产品类别销售额。

multi_polar_area_chart.py
import matplotlib.pyplot as plt
import numpy as np

# Data: Angles (seasons) and sales (thousands $)
theta = np.radians([0, 90, 180, 270, 360])  # Winter, Spring, Summer, Fall, Winter
electronics = [30, 35, 40, 45, 30]
clothing = [25, 20, 15, 30, 25]

# Create a polar chart
ax = plt.subplot(111, projection="polar")
ax.fill_between(theta, electronics, color="skyblue", alpha=0.4, label="Electronics")
ax.fill_between(theta, clothing, color="lightgreen", alpha=0.4, label="Clothing")

# Customize the chart
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_xticks(theta[:-1])  # Exclude the last tick to avoid overlap
ax.set_xticklabels(["Winter", "Spring", "Summer", "Fall"])
ax.set_title("Seasonal Sales by Category (2025)")
ax.legend()

# Display the chart
plt.show()

此图比较了 2025 年电子产品和服装的季节性销售额。 两次 fill_between 调用创建重叠的填充区域,其中 alpha=0.4 确保两个数据集的可见性。 角度表示季节,转换为弧度,自定义 xticklabels 将数值刻度替换为季节名称以提高清晰度。

电子产品销售额在夏季 (40) 和秋季 (45) 达到高峰,可能是由于技术发布,而服装销售额在秋季 (30) 和冬季 (25) 达到高峰,反映了季节性需求。 这种可视化突出了周期性趋势和比较,使其对零售分析或营销策略规划有价值。

具有尺寸变化的极坐标散点图

此示例显示如何创建一个具有不同点大小的极坐标散点图,以表示按方向划分的地震震级。

polar_scatter_chart.py
import matplotlib.pyplot as plt
import numpy as np

# Data: Directions (degrees), distances (km), and magnitudes
theta = np.radians([45, 135, 225, 315])  # NE, SE, SW, NW
distances = [50, 80, 60, 70]  # Distance from epicenter
magnitudes = [4.5, 6.0, 5.2, 4.8]  # Richter scale

# Create a polar scatter chart
ax = plt.subplot(111, projection="polar")
scatter = ax.scatter(theta, distances, s=np.array(magnitudes) * 50, color="red", alpha=0.6, label="Earthquakes")

# Customize the chart
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_title("Earthquake Magnitudes by Direction (March 2025)")
ax.set_xlabel("Direction from Epicenter")
ax.set_ylabel("Distance (km)")
ax.legend()

# Display the chart
plt.show()

此极坐标散点图绘制了 2025 年 3 月地震震中的地震,方向以度为单位,距离以公里为单位,震级以里氏震级表示。 scatter 函数绘制点,其中 s=np.array(magnitudes) * 50 按震级缩放点大小以产生视觉冲击。 alpha=0.6 参数为重叠的点添加透明度。

最大的地震 (6.0) 发生在东南 80 公里处,而最小的地震 (4.5) 发生在东北 50 公里处。 这种图表类型擅长显示空间关系和强度,对地震学家或应急规划人员评估相对于中心点的地震分布很有用。

极坐标图的最佳实践

来源

Matplotlib 极坐标图文档

在本文中,我们探讨了使用 Matplotlib 的各种类型的极坐标图,包括基本极坐标图、极坐标条形图、多数据集面积图和具有尺寸变化的散点图。

作者

我叫 Jan Bodnar,是一位充满激情的程序员,拥有丰富的编程经验。 自 2007 年以来,我一直在撰写编程文章。 迄今为止,我已经撰写了超过 1,400 篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出 所有 Python 教程