python 画图操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_20183817/article/details/81945166
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
# ax1=fig.add_subplot(2,3,1)
ax2=fig.add_subplot(2,3,6)
plt.plot([1.5,1.5,1.9,1.7])#这个在哪个图下面就在哪个画图
ax2=fig.add_subplot(2,3,5)
# ax2=fig.add_subplot(2,3,4)
ax2=fig.add_subplot(2,3,3)
ax2=fig.add_subplot(2,3,2)
ax2=fig.add_subplot(2,3,1)
# ax1=fig.add_subplot(2,2,4)
# ax1=fig.add_subplot(2,2,3)
plt.plot([1.5,1.5,1.9,1.7])

add_subplot 前面两个参数 2*3个图 1-6分别为这个6个图

用于画空白图

 与上面的效果类似

fig,axes=plt.subplots(2,2,sharex=True,sharey=True)
# axes[0,0].hist(randn(500),bins=50,color='k',alpha=0.5)
# axes[0,1].hist(randn(500),bins=50,color='k',alpha=0.5)
# axes[1,0].hist(randn(500),bins=50,color='k',alpha=0.5)
# axes[1,0].hist(randn(500),bins=50,color='k',alpha=0.5)
# print randn(2)
for i in [0,1]:
    for j in [0,1]:
        axes[i,j].hist(randn(500),bins=50,color='k',alpha=0.5)
plt.subplots_adjust(wspace=0,hspace=0)

plt.plot(randn(30).cumsum(),'r--') #红色
plt.plot(randn(30).cumsum(),'ko--')#元黑点
plt.plot(randn(30).cumsum(),'g--')#绿色

 

 

设置标题、轴标签、刻度以及刻度标签 

fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(randn(800).cumsum())
ticks=ax.set_xticks([0,250,500,750,1000])#修改x轴的刻度
labels=ax.set_xticklabels(['one','two','three','four','five'],rotation=30,fontsize='small')
#设置x轴对应的名字
ax.set_title('my first picture')#设置图的名称
ax.set_xlabel('stages')#设置x轴名称
ax.set_ylabel('values')#设置y轴名称

 

 画图举例说明

import numpy as np
import matplotlib.pyplot as plt
p=np.arange(0.001,1,0.001,dtype=np.float)
#生成一个步长为0.001的array
gini=2*p*(1-p)
h=-(p*np.log2(p)+(1-p)*np.log2(1-p))/2
err=1-np.max(np.vstack((p,1-p)),0)
plt.plot(p,h,'b--',linewidth=2,label='entropy')
#linewidth=2 线的宽度为2
#label='entropy' 线的标签 与plt.legend 搭配使用
plt.plot(p,gini,'r-',linewidth=2,label='gini')
plt.plot(p,err,'g:',linewidth=2,label='err')
#'g:' 表示绿色虚线  r- 红色实线  'b--' 蓝色破折线
plt.grid(True)# 格子打开
plt.legend(loc='upper left') #python画图matplotlib的Legend(显示图中的标签) 左上角
print 'type is ',type(p)
plt.show()

 

 shape 和reshape的使用

import numpy as np
z = np.array([[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9, 10, 11, 12],
          [13, 14, 15, 16]])

In [3]:
z

Out[3]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
In [11]:

a=z.reshape(-1, 1)

In [10]:

z.shape

Out[10]:
(4L, 4L)
In [12]:

a.shape

Out[12]:
(16L, 1L)

猜你喜欢

转载自blog.csdn.net/baidu_20183817/article/details/81945166