Matplotlib画图基础

Matplotlib画图
目录:
1.根据数据画曲线图
2.带箭头图
3.3D立体图
4.多子图结构
5.饼状图
6.柱状图


import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
data= np.loadtxt('/home/robot_lcl/lcl_file/train_log/train.log.test')
#print data
x=data[:,0]
y1=data[:,3]

y2=data[:,2]
#plt.figure(1,figsize=(3,2))
fig = plt.figure(figsize=(3,2))
ax1 = fig.add_subplot(111)
plot1, = ax1.plot(x, y1, 'b', label='$loss')
ax1.set_ylabel('test loss')
ax1.set_xlabel('iteration')

ax2 = ax1.twinx()
plot2, = ax2.plot(x, y2, 'r',label='$accuracy')

ax2.set_ylabel('test accuracy')
plt.legend([plot1, plot2], ( 'loss','accuracy' ) , 'upper center',bbox_to_anchor=(0.85, -0.05),shadow=True, ncol=2)
#plt.legend(loc='upper center', bbox_to_anchor=(0.8, -0.05),  shadow=True, ncol=2)


plt.savefig('/home/robot_lcl/lcl_file/train_log/accu.png',format='png')
plt.show()

这里写图片描述

2.===annotat格式可参阅网上资料。在通过annotate()函数画一个标注的箭头;其中的两个位置是箭头和箭尾的坐标,后面是颜色等信息

import matplotlib.pyplot as plt
fig = plt.figure(5)
plt.figure(1,figsize=(3,2))   #控制宽度和高度;


ax = plt.subplot(111)


ann = ax.annotate("Test",
                 xy=(0.2,0.2),
                 xytext=(0.8,0.8),
                 size=20, va="center", ha="center",
                 bbox=dict(boxstyle="round4", fc="w"),  #字体大小,位置,边框以及前景色
                 arrowprops=dict(arrowstyle="-|>",connectionstyle="arc3,rad=0.2",fc="r") #曲线,箭头红色


                 )
ax.grid(True)
plt.show()

这里写图片描述

<matplotlib.figure.Figure at 0x7ff31dee1e90>

3.   3D立体图

import numpy as np  
import matplotlib.pyplot as plt  
import mpl_toolkits.mplot3d  
x,y=np.mgrid[-2:2:20j,-2:2:20j]  
z=x*np.exp(-x**2-y**2)  

ax=plt.subplot(111,projection='3d')  
ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8)  
ax.set_xlabel('x')  
ax.set_ylabel('y')  
ax.set_zlabel('z')  

plt.show()  

这里写图片描述
png

  1. 画多子图结构
from matplotlib import pyplot as plt
import numpy as np

t=np.arange(0, 5, 0.2) #类似python里的range

fig=plt.figure()

#行,列,序号
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(212)

ax1.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
ax1.grid(True)
ax1.set_title("plot")


ax2.semilogy(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
ax2.grid(True)
ax2.set_title("ylog")


ax3.semilogy(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
ax3.grid(True)
ax3.set_title('loglog')

fig.suptitle('normal vs ylog vs loglog')   #大图的标题
fig.subplots_adjust(hspace=0.4)   #控制图3和上两个图间距;

plt.show()

这里写图片描述

多子图结构画法:
from matplotlib import pyplot as plt
import numpy as np

t=np.arange(0, 5, 0.2) #类似python里的range

fig=plt.figure()

#行,列,序号
ax1 = fig.add_subplot(321)
ax2 = fig.add_subplot(322)
ax3 = fig.add_subplot(312)
ax4 = fig.add_subplot(325)
ax5 = fig.add_subplot(326)

这里写图片描述

5.画饼状图


plt.figure(1,figsize=(3, 3))
sizes = [120,30,59]
labels = 'GL', 'GR', 'GS'
colors = ['red','yellowgreen','lightskyblue']

explode=(0.06, 0, 0)

patches, l_texts, p_texts = plt.pie(sizes, explode=explode, labels=labels, colors=colors,labeldistance=1.1,autopct='%2.1f%%', shadow=True,
        startangle=90, pctdistance=0.6)
# labeldistance 标签距圆心1.1倍半径, autopct 2位整数,1位小数;  startangle第一块起始角度,逆时针90开始;
# pctdistance百分比距离圆心位置;

plt.axis('equal')#设置 XY轴刻度一致,保证图是圆的;
#plt.legend()  # 图例

#patches, l_texts, p_texts=  设置标签,比例数字显示的大小;
for t in l_texts:
    t.set_size(10)

for t in p_texts:
     t.set_size(10)

plt.show()

这里写图片描述

6.画柱状图

import matplotlib.pyplot as plt; 
import numpy as np

feat = [0.6,0.8,0.4]
print feat

plt.figure(num=1,figsize=(2.5, 2)) 
method = ('Left','Right', 'Go')
x_pos = np.arange(3)

#plt.barh(range(len(feat)), feat)
plt.bar(x_pos,feat,align = 'center', alpha = 0.5,color='r',width=0.3)  

plt.xticks(x_pos,method,fontsize =8)  
plt.yticks(fontsize =8)   # change the num axis size
plt.title('Output',size=8) 
#plt.ylabel("prob")
plt.grid(True)  #网格线

for x, y in zip(x_pos,feat):
    plt.text(x,y+0.02, '%.2f' % y, ha='center', va='bottom')  # 将数字加在各个柱状图上;

plt.ylim(0,1.1)    
#plt.savefig('/home/robot_lcl/Desktop/picture/p.png',format='png')
plt.show()  
[0.6, 0.8, 0.4]

这里写图片描述

import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.figure(num=1,figsize=(2.5, 2)) 
plt.barh(range(len(data)), data)



plt.show()

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值