Introduction to Matplotlib library

Matplotlib is an excellent third-party library for data visualization in python. Can draw coordinate system, pie chart, etc. more than one hundred forms of effects.

The Matplotlib library is composed of various visualization classes, with a complex internal structure, inspired by matlab.

Matplotlib.pyplot is a command sub-library for drawing various visual graphics, Which is equivalent to a shortcut. You can simply call all the visualization methods in Matplotlib. Because the name is too long, the alias plt is introduced.

import matplotlib.pyplot as plt

Test one:

Enable spyder to draw graphics.

import matplotlib.pyplot as plt

plt.plot([3, 1, 4, 5, 2])
plt.ylabel("grade") #此时默认x轴是y轴元素的索引,自动生成
plt.savefig('test', dpi=600) #将输出图形存储为文件,默认PNG格式,可以通过dpi修改输出质量
plt.show()

Insert picture description here

Test two:

Both the horizontal and vertical coordinates are set.

import matplotlib.pyplot as plt

plt.plot([0, 2, 4, 6, 8], [3, 1, 4, 5, 2])
plt.ylabel("grade") #此时默认x轴是y轴元素的索引,自动生成
plt.axis([-1, 10, 0, 6]) #横坐标起始于-1,终止于10。纵坐标起始于0,终止于6。
plt.savefig('test', dpi=600) #将输出图形存储为文件,默认PNG格式,可以通过dpi修改输出质量
plt.show()
  1. plt.plot(x,y) When there are more than two parameters, plot the data points in the order of x-axis and y-axis.
  2. plt.axis() is a function to set the scale of the horizontal and vertical coordinates.
    Insert picture description here

Test three:

pyplot drawing area

plt.subplot(3,2,4) refers to dividing the plane into three rows and two columns, a total of six areas, 4 represents the fourth area.

This function creates a partition system in the global drawing area and locates a sub-drawing area.

This function can remove all the commas in the parameters, expressed as plt.subplot(324).

import matplotlib.pyplot as plt
import numpy as np

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t) #衰减函数

a = np.arange(0.0, 5.0, 0.02)

plt.subplot(211)
plt.plot(a,f(a))

plt.subplot(2,1,2)
plt.plot(a, np.cos(2*np.pi*a),'r--') #以虚线的方式绘制,一个正弦曲线
plt.savefig('test',dpi=700)
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42253964/article/details/106538759