MatplotLib simple example program library

MatplotLib Python library is the most popular third-party graphics library, his most imitated API design from matlab. Therefore matplotlib using matlab is very similar.

The following program segment can be run in a variety of environments, it is recommended that runs under spyder.

 

  • One-dimensional, two-dimensional graphics and data preservation
import matplotlib.pyplot as plt
plt.plot([3,2,1,4,5])
plt.ylabel("grade")
plt.savefig("test",dpi=200) # dpi(Dots Per Inch,每英寸点数,代表图形质量)
plt.show() # 在IPython环境下展示图像

For one-dimensional data, the default value for the vertical list, the list and the index is the abscissa. Drawing results were as follows 

To achieve the above drawing in matlab, the following code should be written, both of which may be compared.

a=[3,2,1,5,4];
plot(a);

 

The following embodiment is a small two-dimensional drawing data:

import matplotlib.pyplot as plt
plt.plot([0,2,4,6,8],[3,2,1,4,5])
plt.ylabel("grade")
plt.axis([-1,10,0,6])
plt.savefig("test",dpi=200) # dpi(Dots Per Inch,每英寸点数,代表图形质量)
plt.show() # 在IPython环境下展示图像

 

 

  •  Dividing the drawing region
import numpy as np
import matplotlib.pyplot as plt


def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)


a=np.arange(0,5,0.02)
b=f(a)
plt.subplot(2,1,1) # 也可以简写为plt.subplot(212)
plt.plot(a,b)
plt.subplot(2,1,2)
plt.plot(a,np.cos(2*np.pi*a),'r--')

plt.show()

 

 

Published 29 original articles · won praise 6 · views 3402

Guess you like

Origin blog.csdn.net/qq_42138454/article/details/104080125