Python uses matplotlib.pyplot to generate graphics and charts

Article directory

Insert image description here

introduce

matplotlib.pyplotis a submodule of the Matplotlib library that provides a simple interface to create various types of graphs and charts. Using it pyplot, you can easily create, customize, and display graphics without writing a lot of low-level code. Here are matplotlib.pyplotsome common uses of:

  1. Import the module: First, you need to import matplotlib.pyplotthe module. Typically, it is imported as plt, which is a common convention:

    import matplotlib.pyplot as plt
    
  2. Create Chart: Use plt.figure()to create a new chart. If you do not create a chart explicitly, pyplota default chart is automatically created.

    plt.figure()
    
  3. Plot data: Use plt.plot()to plot data. You can pass in the X-axis and Y-axis data as parameters, and set the line style, color, markers, etc. through optional parameters. For example:

    x = [1, 2, 3, 4, 5]
    y = [10, 15, 13, 18, 16]
    plt.plot(x, y, marker='o', linestyle='-')
    
  4. Add labels and titles: Use plt.xlabel(), plt.ylabel()and plt.title()to add labels for the X-axis and Y-axis and a title for the chart:

    plt.xlabel('X轴标签')
    plt.ylabel('Y轴标签')
    plt.title('图表标题')
    
  5. Customize chart appearance: You can customize the appearance of your chart using various methods, including setting line widths, colors, markers, scales, labels, and more. For example:

    plt.plot(x, y, marker='o', linestyle='-', color='blue', linewidth=2, label='数据1')
    
  6. Legend: If you have multiple data sets in your chart, you can use plt.legend()to add a legend to differentiate between them. Parameters need to be added for each data set when plotting labeland then called plt.legend()to display the legend.

    plt.plot(x1, y1, label='数据集1')
    plt.plot(x2, y2, label='数据集2')
    plt.legend()
    
  7. Save Chart: Use plt.savefig()to save the chart as an image file for later use or sharing.

    plt.savefig('my_plot.png')
    
  8. Display the chart: Finally, use plt.show()to display the chart. This function is usually called after you have completed all chart customization.

    plt.show()
    

This is just matplotlib.pyplotbasic usage. Matplotlib also provides more advanced features, such as subplots, 3D plots, text annotations, color mapping, etc. To learn more details, it is recommended to consult Matplotlib's official documentation and example library.

use

Let’s look at a code first

import matplotlib.pyplot as plt

# 数据
频率 = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
周期 = [10, 5, 3.36, 2.48, 2, 1.68, 1.44, 1.28, 1.12, 1]
#用来能够显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 创建图表
plt.figure(figsize=(8, 6))
plt.plot(频率, 周期, marker='o', linestyle='-')
plt.title('周期随着频率变化')
plt.xlabel('频率(Hz)')
plt.ylabel('周期(ms)')
plt.grid(True)

# 显示图表
plt.show()

Show results
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_74850661/article/details/132946396