matplotlib库学习(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013817676/article/details/79027888
#matplotlib模块学习
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5,5,50)
y1 = 2*x + 1
y2 = x**2
#画一个直线
plt.figure()
plt.plot(x,y1)
plt.show()

这里写图片描述

#在同一个fig中画两条线
plt.figure(num=3,figsize=(8,6)) #num对应图的序号,figsize对应图的大小
plt.plot(x,y2)
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')

plt.show()

这里写图片描述

#设置坐标轴区间
plt.figure()
plt.plot(x, y2)
# plot the second curve in this figure with certain parameters
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
# set x limits
plt.xlim((-1, 2))
plt.ylim((-2, 3))
plt.xlabel('x axis')
plt.ylabel('y axis')

#修改坐标项
new_ticks = np.linspace(-1,2,5)
print new_ticks
#修改x轴的范围
plt.xticks(new_ticks)
#设置
plt.yticks(new_ticks,[r'$apple$',r'$banana$',r'$pear$',r'$food\ food$',r'rice'])
plt.show()
[-1.   -0.25  0.5   1.25  2.  ]
#修改坐标轴的样子
ax = plt.gca() #获取当前坐标轴
ax.spines['right'].set_color('none') #删除坐标顶部横线
ax.spines['top'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
# ACCEPTS: [ 'top' | 'bottom' | 'both' | 'default' | 'none' ]

ax.spines['bottom'].set_position(('data', 0)) #设置Y轴的坐标原点
# the 1st is in 'outward' | 'axes' | 'data'
# axes: percentage of y axis
# data: depend on y data

ax.yaxis.set_ticks_position('left') 
# ACCEPTS: [ 'left' | 'right' | 'both' | 'default' | 'none' ]

ax.spines['left'].set_position(('data',0)) #设置X轴的坐标原点
plt.show()

这里写图片描述

plt.figure()
# set x limits
plt.xlim((-1, 2))
plt.ylim((-2, 3))

# set new sticks
new_sticks = np.linspace(-1, 2, 5)
plt.xticks(new_sticks)
# set tick labels
plt.yticks([-2, -1.8, -1, 1.22, 3],
           [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])

l1, = plt.plot(x, y1, label='linear line')
l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='square line')
#设置legend
plt.legend(handles=[l1,l2],labels=['line1','line2'],loc='best') 
"""legend( handles=(line1, line2, line3),
           labels=('label1', 'label2', 'label3'),
           'upper right')
    The *loc* location codes are::
          'best' : 0,          (currently not supported for figure legends)
          'upper right'  : 1,
          'upper left'   : 2,
          'lower left'   : 3,
          'lower right'  : 4,
          'right'        : 5,
          'center left'  : 6,
          'center right' : 7,
          'lower center' : 8,
          'upper center' : 9,
          'center'       : 10,"""
plt.show()

这里写图片描述

#添加注解
#先画出一条直线

x = np.linspace(-3,3,50)
y = x*2 + 1

plt.figure()
plt.plot(x,y)

#修改坐标轴
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()

这里写图片描述

#添加点
x0 = 1
y0 = 2*x0 + 1
#画一个点
plt.scatter(x0,y0,s = 50,color='b')
#画一天直线
plt.plot([x0,x0,],[y0,0,],'k--',linewidth=2.5)

这里写图片描述

#annotate 1
plt.annotate(r'$2x+1=%s'%y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),
            textcoords='offset points',fontsize=16,
            arrowprops=dict(arrowstyle='->',connectionstyle="arc3,rad=.2"))
#annotate 2
plt.text(-3.5, 3, r'$This\ is\ the\ some\ text.$',
         fontdict={'size': 16, 'color': 'r'})

这里写图片描述

#散点图
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
C = np.arctan2(Y,X) # for color value

plt.scatter(X,Y,s=75,c=C,alpha=0.5)

plt.xlim((-1.5,1.5))
plt.ylim((-1.5,1.5))
#设置x,y轴影藏
plt.xticks(())
plt.yticks(())
plt.show()

这里写图片描述

#柱状图
n  = 12
X = np.arange(n)
Y1 = (1-X/float(n))*np.random.uniform(0.5,1,n)
Y2 = (1-X/float(n))*np.random.uniform(0.5,1,n)

plt.bar(X,Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')

for x,y in zip(X,Y1):
    #ha:horizatal alignment
    plt.text(x,y+0.05,'%.2f'%y,ha='center',va='bottom')

for x,y in zip(X,Y2):
    #ha:horizatal alignment
    #va:vertial alignment
    plt.text(x,-y-0.05,'-%.2f'%y,ha='center',va='top')

plt.xlim(-0.5,n)
plt.ylim(-1.25,1.25)
plt.xticks(())
plt.yticks(())

plt.show()

这里写图片描述

#等高线
def f(x,y):
    #计算高度函数
    return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)

n= 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X,Y = np.meshgrid(x,y)

#10代表登高线条数-1
plt.contour(X,Y,f(X,Y),10,alpha=0.75,cmap=plt.cm.hot)
#画线上数字
C = plt.contour(X,Y,f(X,Y),10,color='black',linewidth=.5)
#添加到等高图上
plt.clabel(C,inline=True,fontsize=10)

plt.xticks(())
plt.yticks(())
plt.show()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013817676/article/details/79027888
今日推荐