博客
关于我
matplotlib常见基础用法总结
阅读量:802 次
发布时间:2019-03-25

本文共 4027 字,大约阅读时间需要 13 分钟。

Matthew is not just about static graphs, it's also about dynamic interactive plots. Starting from the simplest usage:

import matplotlib.pyplot as pltplt.plot([1, 2, 3, 4], [3, 6, 7, 9], 'r--')plt.xlabel('X-Axis')plt.ylabel('Y-Axis')plt.title('Interactive Plot Example')plt.show()

This code draws a simple line graph with red dashed lines, labels for both axes, and a title. Below are some advanced examples toward greater customization:

Customizing Plot Styling

plt.rcParams['font.sans-serif'] = ['Arial']  # Set default fontplt.figure(figsize=(10, 6))  # Set figure sizeplt.plot([1, 2, 3, 4], [3, 6, 7, 9], color='red', linestyle='--', linewidth=2, marker='o')plt.xlabel('X Axis', fontsize=12)plt.ylabel('Y Axis', fontsize=12)plt.title('Interactive Plot Example', fontsize=14, color='black')plt.grid(True, which=1, color='black', linestyle='--', linewidth=1)plt.show()

Key elements in the above code:

  • -figure size: Adjusted the size of the plot
  • -color and style: Specified color, linestyle, and linewidth
  • grid: Added a grid with custom settings
  • ** fonts**: Set default font to Arial for better readability

Combining Multiple Plots

# Create two subplots within one figureplt.figure(figsize=(10, 6))plt.subplot(2, 1, 1)plt.plot([1, 2, 3, 4], [3, 6, 7, 9], color='red', linestyle='-', linewidth=2)plt.xlabel('Time', fontsize=10)plt.ylabel('Temperature', fontsize=10)plt.title('Temperature vs Time', fontsize=12)plt.subplot(2, 1, 2)plt.plot([1, 2, 3, 4], [5, 4, 3, 2], color='green', linestyle='--', linewidth=1)plt.xlabel('Time', fontsize=10)plt.ylabel('Pressure', fontsize=10)plt.title('Pressure vs Time', fontsize=12)plt.show()

Key elements:

  • Figure and Subplot creation: Usingplt.figure() and subplot() for multiple graphs in one figure
  • Labels: Customized labels with specified font sizes
  • Titles: Added descriptive titles to each subplot

Customizing Axes Ranges and Limits

import numpy as npplt.figure(figsize=(8, 6))x = np.linspace(0, 10, 100)y = np.sin(x)plt.plot(x, y, color='blue', linestyle='-', linewidth=2)plt.xlabel('X (Degrees)', fontsize=10)plt.ylabel('Sine Wave', fontsize=10)plt.title('Sine WavePlot', fontsize=12)plt.xlim(0, 10)  # Set x-axis limitsplt.ylim(-1, 1)  # Set y-axis limitsplt.grid(True, which=1, color='black', linestyle='--', linewidth=0.5)plt.show()

Key elements:

  • Axes limits: Using xlim() and ylim() to set visible range of axes
  • Grid: Added minor grid lines
  • Custom titles and labels: Enhanced readability through labels and titles

Adding Annotations and Labels

# Example: Annotating points on the graphplt.figure(figsize=(8, 6))x = np.arange(0, 2, 0.1)y = np.random.rand(len(x))plt.plot(x, y, color='yellow', linestyle='-', linewidth=2, markersize=4, marker='o')plt.xlabel('X', fontsize=10)plt.ylabel('Random Values', fontsize=10)plt.title('Random Scatter Plot', fontsize=12)plt.grid(True, which=1, color='black', linestyle='--', linewidth=0.5)for i, (xi, yi) in enumerate(zip(x, y)):    plt.text(xi-0.05, yi+0.02, f'({xi:.2f}, {yi:.2f})', ha='right', va='bottom')plt.show()

Key elements:

  • Annotations: Added text annotations for each data point
  • Markers: Customized markers and sizes
  • Labels and title: Enhanced readability through proper annotation

subordinate curtain cleaning

# Create an interactive plot that includes annotations and multiple elementsimport numpy as npimport matplotlib.pyplot as pltplt.figure(figsize=(10, 6))x = np.arange(-5, 5, 0.5)y = np.arange(-5, 5, 0.5)plt.scatter(x, y, color='blue', s=5, marker='o')plt.xlabel('X', fontsize=10)plt.ylabel('Y', fontsize=10)plt.title('Scatter Plot with Annotations', fontsize=12)plt.grid(True, which=1, color='black', linestyle='--', linewidth=0.5)# Add an annotation to a specific pointplt.text(2, 3.5, 'Important Point', ha='center', va='bottom', color='white', backgroundcolor='black')plt.show()

Key elements:

  • Interactive elements: Added an annotation to highlight a specific point
  • Custom plot styling: Adjusted grid and point styles
  • Labels and titles: Consistent and readable

Matthew.

这篇文章是关于 matplotlib 学习指南的。其中涵盖了从最基础的绘图到更复杂的自定义化的内容。

转载地址:http://syayk.baihongyu.com/

你可能感兴趣的文章
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>