请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365810 新帖:17

matplotlib 数据可视化 - 线性图

爱德华发表于:5 月 10 日 00:41回复(1)

该文章为本人的学习笔记,笔记为原创,内容为参考(*^__^*) 嘻嘻……

线性图、条状图、饼状图

import numpy as np
import matplotlib.pyplot as plt 

线性图¶

线性图的各个数据点是由一条线来连接的。

如,将函数 y=sin(3*x)/x 展示在图表上。

'''创建x数据'''
x = np.arange(-2*np.pi,2*np.pi,0.01)  # 因为是正弦函数,所以x的值应该是pi的倍数或因数
y = np.sin(3*x)/x
plt.plot(x,y)
plt.show()

对上例进行扩展

显示y=sin(n*x)/x图像

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y)
plt.plot(x,y2)
plt.plot(x,y3)
plt.show()

上例中,我们绘制了三条线,可以看出,matplotlib会自动为不同的线赋予不同的颜色

尝试使用关键字参数color来指定线条的颜色,用linestyle来指定线型

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,'k--',linewidth=3)  # 这是我们之前了解到的
plt.plot(x,y2,'m-.')
plt.plot(x,y3,color='#87a3cc',linestyle='--')
plt.show()

之前,在plot()的第三个参数中,用r可以表示红色,g表示绿色,b表示蓝色,这里列举一些常用的颜色字符代表:

  • b 蓝色
  • g 绿色
  • r 红色
  • c 蓝绿色
  • m 洋红
  • y 黄色
  • k 黑色
  • w 白色

将上图中的x轴变成π的倍数

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,'k--',linewidth=3)
plt.plot(x,y2,'m-.')
plt.plot(x,y3,color='#87a3cc',linestyle='--')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.show()

改变y轴的刻度值

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,'k--',linewidth=3)
plt.plot(x,y2,'m-.')
plt.plot(x,y3,color='#87a3cc',linestyle='--')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])
plt.show()

总结:在设置x/y轴刻度的时候,ticks()中的参数,第一个列表表示刻度范围,第二个参数表示标签,用LeTeX表达式书写

接下来,尝试改变轴的位置¶

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,color='b')
plt.plot(x,y2,color='r')
plt.plot(x,y3,color='g')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])

'''通过gac区取Axes对象,即获取画轴的对象'''
ax = plt.gca()
print(ax)

plt.show()
AxesSubplot(0.125,0.125;0.775x0.755)
x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,color='b')
plt.plot(x,y2,color='r')
plt.plot(x,y3,color='g')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])

'''通过gac区取Axes对象,即获取画轴的对象'''
ax = plt.gca()
'''spines表示画轴的边对象,这里设置右边和上边轴为无色'''
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
print(ax.spines)

plt.show()
OrderedDict([('left', <matplotlib.spines.Spine object at 0x7f35587482b0>), ('right', <matplotlib.spines.Spine object at 0x7f3558609a90>), ('bottom', <matplotlib.spines.Spine object at 0x7f3558622630>), ('top', <matplotlib.spines.Spine object at 0x7f35585a16a0>)])
x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,color='b')
plt.plot(x,y2,color='r')
plt.plot(x,y3,color='g')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])

'''通过gac区取Axes对象,即获取画轴的对象'''
ax = plt.gca()
'''spines表示画轴的边对象,这里设置右边和上边轴为无色'''
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
'''set_position指定某个轴在哪个位置,这里指定在数据的0,即穿过0'''
ax.spines['bottom'].set_position(('data',0))

plt.show()
x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,color='b')
plt.plot(x,y2,color='r')
plt.plot(x,y3,color='g')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])

'''通过gac区取Axes对象,即获取画轴的对象'''
ax = plt.gca()
'''spines表示画轴的边对象,这里设置右边和上边轴为无色'''
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
'''set_position指定某个轴在哪个位置,这里指定在数据的0,即穿过0'''
ax.spines['bottom'].set_position(('data',0))
'''然后设置y轴的名称,并让它也穿过0'''
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

plt.show()

完整代码:

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,color='b')
plt.plot(x,y2,color='r')
plt.plot(x,y3,color='g')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

plt.show()

用箭头将公式标签指向某一点¶

使用到的函数是annotate()

x = np.arange(-2*np.pi,2*np.pi,0.01)
y = np.sin(3*x)/x
y2 = np.sin(2*x)/x
y3 = np.sin(1*x)/x
plt.plot(x,y,color='b')
plt.plot(x,y2,color='r')
plt.plot(x,y3,color='g')
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],['$-2\pi$','$-\pi$','$0$','$1\pi$','$2\pi$'])
plt.yticks([-1,0,1,2,3],['$-1$','$0$','$1$','$2$','$3$'])

plt.annotate(r'$\lim_{x\to 0}\frac{\sin(x)}{x}=1$',  # 表达式
             xy=[0,1],  # 坐标
             xycoords='data',  
             xytext=[30,30],  # 文本坐标
             fontsize=16,  # 字体大小 
             textcoords='offset points',
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3, rad=.2"))  # 指定箭头

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
plt.show()

为pandas数据结构绘制线性图¶

import pandas as pd

data = {'one':[1,3,4,5,5],
       'two':[2,4,5,2,7],
       'three':[3,2,4,8,9]}
df = pd.DataFrame(data)
x = np.arange(5)
plt.axis([0,5,0,7])
plt.plot(x,df)
plt.legend(data, loc=2)
plt.show()
 

全部回复

0/140

量化课程

    移动端课程