6.20学习笔记(matplotlib)

import matplotlib.pyplot as mpl
from pylab import *
import datetime
import numpy as np
fig=figure()
ax=gca()														//gca:坐标轴相关操作
#时间区间
start = datetime.datetime(2019,3,11)
stop=datetime.datetime(2019,3,29)
delta=datetime.timedelta(days=1)						//时间间隔:以1天为单位
dates=mpl.dates.drange(start,stop,delta)
values=np.random.rand(len(dates))
ax.plot_date(dates,values,ls='-')
date_format=mpl.dates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()
show()

在这里插入图片描述

from matplotlib.pyplot import *
import numpy as np
x1=np.random.normal(30,2,100)				//2到100之间的30个数
plot(x1,label='plot')
#图例
#图标的起始位置,宽度,高度,归一化坐标
#loc可选,为了图标不覆盖图
#ncol 图例个数
#图例平铺
#坐标轴和图例边界之间的间距
legend(bbox_to_anchor=(0.,1.02,1.,.102),loc=4,ncol=1,mode="expand",borderaxespad=0.1)
#注解
#import data 注释
#(55,30)要关注的点
#xycoords='data'注释和数据使用相同坐标系
#xytest 注释的位置
#arrowprops注释用的箭头
annotate("Import data",(55,30),xycoords='data',xytext=(5,35),arrowprops=dict(arrowstyle='->'))
show()

在这里插入图片描述
直方图

import matplotlib.pyplot as plt
import numpy as np
mu=100					\\μ
sigma=15				\\标准差
x=np.random.normal(mu,sigma,10000)						\\一万个点
ax=plt.gca()
ax.hist(x,bins=30,color='g')										\\30个分箱
ax.set_xlabel('v')
ax.set_ylabel('f')
ax.set_title(r'$mathrm{Histogram:}\ \mu=%d,\ \sigma=%d$' % (mu,sigma))
plt.show()

在这里插入图片描述
饼图

from pylab import *
figure(1,figsize=(6,6))
ax=axes([0.1,0.1,0.8,0.8])
labels='sping','summer','autumn','winter'
x=[15,30,45,10]
#explode=(0.1,0.2,0.1,0.1)
explode=(0.1,0,0,0)						//突出程度
pie(x,explode=explode,labels=labels,autopct='%1.1f%%',startangle=67)
title('rainy days by season')
show()

在这里插入图片描述
坐标轴设置

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-np.pi,np.pi,500,endpoint=True)
y=np.sin(x)
plt.plot(x,y)
ax=plt.gca()
#top bottom left right 四条线段框成的
#上下边界颜色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('r')
#坐标轴位置
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
#坐标轴上刻度位置
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.grid()
plt.show()

在这里插入图片描述
误差条形图

import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,10,1)
y=np.log(x)
xe=0.1*np.abs(np.random.randn(len(y)))
plt.bar(x,y,yerr=xe,align='center',ecolor='r',color='cyan',label='experiment')
plt.xlabel('x')
plt.ylabel('y')
plt.title('measirements')
plt.legend(loc='upper left')
#这种图例用法更直接
plt.show()

在这里插入图片描述

带填充区域的图表

import matplotlib.pyplot as plt
from matplotlib.pyplot import *
import numpy as np
x=np.arange(0,2,0.01)
y1=np.sin(2*np.pi*x)
y2=1.2*np.sin(4*np.pi*x)
fig=figure()
ax=gca()
ax.plot(x,y1,x,y2,color='b')
ax.fill_between(x,y1,y2,where=y2>y1,facecolor='g',interpolate=True)
ax.fill_between(x,y1,y2,where=y2<y1,facecolor='darkblue',interpolate=True)
ax.set_title("filled between")
show()

在这里插入图片描述
散点图

import matplotlib.pyplot as plt
import numpy as np
x=np.random.randn(1000)
y1=np.random.randn(len(x))
y2=1.8+np.exp(x)
ax1=plt.subplot(1,2,1)
ax1.scatter(x,y1,color='r',alpha=.3,edgecolors='white',label='no correl')
plt.xlabel('no correlation')
plt.grid(True)
plt.legend()
ax1=plt.subplot(1,2,2)
#alpha透明度 edgecolors边缘颜色 label图例(结合legend使用)
plt.scatter(x,y2,color='g',alpha=.3,edgecolors='gray',label='correl')
plt.grid(True)
plt.legend()
plt.show()

在这里插入图片描述

12 59:50

发布了5 篇原创文章 · 获赞 2 · 访问量 2672

猜你喜欢

转载自blog.csdn.net/weixin_43621813/article/details/93039933
今日推荐