图/绘制一条直线——matplotlib

本文是一篇关于如何使用Python的Matplotlib库绘制直线图的教程。内容包括如何绘制斜率为2,截距为1的直线,以及如何将坐标轴置于图形中心。此外,还展示了不同的线型和颜色选择,并提供了绘制多条直线的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

The slope equation $y=mx+c$ as we know it today is attributed to René Descartes (AD 1596-1650), Father of Analytic Geometry.

The equation $y=mx+c$y=mx+c represents a straight line graphically, where $m$m is its slope/gradient and $c$c its intercept. In this tutorial, you will learn how to plot $y=mx+b$y=mx+b in Python with Matplotlib.

Consider the straight line $y=2x+1$y=2x+1 , whose slope/gradient is $2$2 and intercept is $1$1. Before we plot, we need to import NumPy and use its linspace() function to create evenly-spaced points in a given interval. In the below example, linspace(-5,5,100) returns 100 evenly spaced points over the interval [-5,5] and this array of points goes as the first argument of the plot()

### 如何在使用 `plot` 绘时添加一条常数线 要在极坐标子中添加一条常数线,可以利用 Matplotlib 的 `axvline()` 或者 `axhline()` 方法。然而,在极坐标系中,这些方法并不直接适用,因为它们通常用于笛卡尔坐标系中的水平或垂直直线。对于极坐标系,可以通过指定固定的角度或者半径来实现类似的常数线效果。 以下是具体实现方式: #### 添加基于角度的常数线 如果希望在特定角度上绘制一条从中心延伸到外圈的直线,则可以直接定义一组数据点 `(theta, r)` 并调用 `plot()` 方法完成此操作。例如,假设要在一个固定的 θ 值处绘制一条直线: ```python import numpy as np import matplotlib.pyplot as plt # 数据准备 r = np.arange(1, 6, 1) # 极径 theta = [i * np.pi / 2 for i in range(5)] # 角度 # 创建极坐标子 fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r, linewidth=3, color='r', label="Original Data") # 主曲线 # 添加基于角度的常数线 (θ = π/4) constant_theta = np.pi / 4 constant_r = np.linspace(0, max(r), 100) # 半径范围从 0 到最大值 ax.plot([constant_theta] * len(constant_r), constant_r, linestyle="--", color="blue", label="Constant Line") # 设置网格并显示像 ax.grid(True) plt.legend() plt.show() ``` 上述代码片段展示了如何通过设定固定的角度 (`π/4`) 和变化的半径值来生成一条贯穿整个极坐标的直线[^1]。 #### 添加基于半径的常数圆环 另一种常见需求是在某一固定半径位置绘制一个完整的圆形轨迹作为参考线。这同样可通过调整输入参数至 `plot()` 实现: ```python # 定义恒定半径下的完整圆周路径 constant_radius = 3.5 circle_thetas = np.linspace(0, 2 * np.pi, 100) # 圆周上的所有可能角度 circle_rs = [constant_radius] * len(circle_thetas) # 将其加入现有形之中 ax.plot(circle_thetas, circle_rs, linestyle="-.", color="green", label="Radius Circle") ``` 以上部分补充到了原程序之后即可得到期望的结果——即围绕着选定距离的一条闭合曲线。 最终综合版如下所示: ```python import numpy as np import matplotlib.pyplot as plt # 数据准备 r = np.arange(1, 6, 1) theta = [i * np.pi / 2 for i in range(5)] # 初始化极坐标环境 fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) # 绘制原始折线 ax.plot(theta, r, linewidth=3, color='red', label="Data Curve") # 描绘角向基准线(斜杠样式) constant_angle = np.pi / 4 radial_values_for_line = np.linspace(0, max(r), endpoint=True, num=100) ax.plot([constant_angle]*len(radial_values_for_line), radial_values_for_line, '--b', label=f"Angle={np.round(constant_angle, decimals=2)} rad") # 渲染径向基准面(虚线形式) fixed_radial_value = 3.5 angular_positions_around_circle = np.linspace(start=0, stop=2*np.pi, num=100) ax.plot(angular_positions_around_circle , [fixed_radial_value]*len(angular_positions_around_circle ), '-.g', label=f"Radial={fixed_radial_value}") # 显示辅助功能与标签说明 ax.set_title('Polar Plot with Constant Lines') ax.grid(True) plt.legend(loc=(0.8,.9)) plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值