matplotlib.pyplot之plot

博客介绍了图形特性fmt的基本格式,fmt = ‘[color][marker][line]’。详细列举了颜色的字符表示,如blue、green等;标记的字符及描述,像point marker、circle marker等;还有线型的字符和对应描述,如实线、虚线等。

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

基本格式:

plot([x], y, [fmt], data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

其中fmt定义图形特性
fmt = ‘[color][marker][line]’

  • character color
    ======= =======
    'b' blue
    'g' green
    'r' red
    'c' cyan
    'm' magenta
    'y' yellow
    'k' black
    'w' white
    maker
  • character description
    ======= =================
    '.' point marker
    ',' pixel marker
    'o' circle marker
    'v' triangle_down marker
    '^' triangle_up marker
    '<' triangle_left marker
    '>' triangle_right marker
    '1' tri_down marker
    '2' tri_up marker
    '3' tri_left marker
    '4' tri_right marker
    's' square marker
    'p' pentagon marker
    '*' star marker
    'h' hexagon1 marker
    'H' hexagon2 marker
    '+' plus marker
    'x' x marker
    'D' diamond marker
    'd' thin_diamond marker
    '|' vline marker
    '_' hline marker

  • linestyle

  • character description
    ========= ===============================
    '-' solid line style
    '--' dashed line style
    '-.' dash-dot line style
    ':' dotted line style
### matplotlib.pyplot.plot 函数的使用方法 `matplotlib.pyplot.plot()` 是 Matplotlib 库中最常用的绘图函数之一,用于绘制线条图和其他形式的图表。它可以根据输入的数据自动生成图形,并支持多种样式选项来自定义外观[^2]。 #### 基本语法 ```python matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs) ``` - `*args`: 输入数据,通常为 `(x, y)` 形式的坐标点集合。 - `scalex`, `scaley`: 是否自动缩放轴的比例,默认为 True。 - `data`: 可选参数,允许传递 Pandas DataFrame 或其他数据容器。 - `**kwargs`: 关键字参数,用于设置线条的颜色、宽度、标记等属性。 --- #### 示例代码 ##### 示例 1:简单的折线图 以下示例展示了如何使用 `plot` 绘制一条基本的折线图。 ```python import matplotlib.pyplot as plt # 数据准备 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 使用 plot 函数绘制折线图 plt.plot(x, y) # 显示图像 plt.show() ``` 此代码会生成一个简单的折线图,其中横坐标为 `x`,纵坐标为 `y`。 --- ##### 示例 2:带样式的折线图 可以通过关键字参数进一步定制线条的样式,例如颜色 (`color`) 和线宽 (`linewidth`)。 ```python import numpy as np import matplotlib.pyplot as plt # 数据准备 x = np.linspace(0, 6, 100) # 自动生成 100 个均匀分布的点 y = np.cos(2 * np.pi * x) * np.exp(-x) + 0.8 # 使用 plot 函数绘制带有样式的折线图 plt.plot( x, y, color='red', # 设置线条颜色为红色 linewidth=3, # 设置线条宽度为 3 linestyle='-' # 设置线条风格为实线 ) # 显示图像 plt.show() ``` 这段代码不仅绘制了曲线,还设置了线条的颜色、宽度以及风格[^1]。 --- ##### 示例 3:多个系列的对比图 可以在同一张图中绘制多条折线以便比较不同数据集之间的关系。 ```python import matplotlib.pyplot as plt # 数据准备 x = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] y2 = [1, 3, 5, 7, 9] # 同一 figure 下绘制两条折线 plt.plot(x, y1, label="Series 1", color="blue") # 添加标签和颜色 plt.plot(x, y2, label="Series 2", color="green") # 添加图例 plt.legend() # 显示图像 plt.show() ``` 这里通过 `label` 参数为每条折线添加了描述性的名称,并调用了 `legend()` 方法显示图例[^3]。 --- ##### 示例 4:散点图与连线组合 如果希望既展示离散点又连接这些点,则可以结合 `marker` 参数实现。 ```python import numpy as np import matplotlib.pyplot as plt # 数据准备 x = np.linspace(0, 10, 50) y = np.sin(x) # 绘制既有标记又有连线的图 plt.plot( x, y, marker='o', # 圆形标记 markersize=5, # 标记大小 linestyle='-.' # 点划线风格 ) # 显示图像 plt.show() ``` 以上代码会在每个数据点处放置圆形标记的同时保留连贯的线条效果[^3]。 --- ### 注意事项 - 当未显式提供 `x` 值时,`plot(y)` 将默认把索引位置作为横坐标值。 - 配置过多复杂选项可能会影响性能;对于大规模数据可视化建议优化渲染策略或改用 Seaborn/SVG 渲染工具替代[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值