Matplotlib简单画图(一) -- plot

官方参考文档:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot
tutorials
https://matplotlib.org/tutorials/introductory/sample_plots.html#sphx-glr-tutorials-introductory-sample-plots-py

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
a = [1,2,3]
# 传入a为x轴,y轴默认
plt.plot(a)
[<matplotlib.lines.Line2D at 0x1258dde4128>]
# 显示
plt.show()

这里写图片描述

a = [1,2,3]
b = [4,5,6]
# 传入x,y轴
plt.plot(a,b)
[<matplotlib.lines.Line2D at 0x1258e0d5ba8>]
plt.show()

这里写图片描述\

# jupyter 中导入这一行可以直接显示图片
%matplotlib inline
plt.plot(a,b)
[<matplotlib.lines.Line2D at 0x1258e39dd68>]

这里写图片描述

# 计算运行时间
%timeit np.arange(10)
570 ns ± 6.99 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
# 修改画出来的图表的样子,*号图
plt.plot(a,b,'*')
[<matplotlib.lines.Line2D at 0x1258e419438>]

这里写图片描述

# 红色的虚线
plt.plot(a,b,'r--')
[<matplotlib.lines.Line2D at 0x1258f66d390>]

这里写图片描述

# 蓝色的虚线
plt.plot(a,b,'b--')
[<matplotlib.lines.Line2D at 0x1258f6ceef0>]
# c传入两个图表
c = [10,8,6]
d = [1,8,3]
plt.plot(a,b,c,d)

这里写图片描述

# 修改线的形状
plt.plot(a,b,'b--',c,d,'r*')
[<matplotlib.lines.Line2D at 0x1258f865a20>,
 <matplotlib.lines.Line2D at 0x1258f865be0>]

这里写图片描述

# 画一个正弦函数
t = np.arange(0.0, 2., 0.1)
t.size
Out[]:20
s = np.sin(t*np.pi)
plt.plot(t,s,'r--')
[<matplotlib.lines.Line2D at 0x1258f937b38>]

这里写图片描述

# 画两条线,并设置x,y轴的label和title
plt.plot(t,s,'r--', t*2, s, '--')
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title('this is a demo')
# 需要有图例的名字才能显示
plt.legend()

这里写图片描述

# 设置每个图的label
plt.plot(t,s,'r--', label='aaaa')
plt.plot(t*2, s, '--',label='bbbb')
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title('this is a demo')
plt.legend()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39778570/article/details/81137741