from matplotlib import pyplot as pltimport pandas as pdimport numpy as npimport seabornplt.rc('figure',figsize=(10,5))seaborn.set()x = np.linspace(0,2,10) #在指定的间隔范围内返回均匀间隔的数字 #linspace(start,end,返回的数量[默认50])plt.plot(x,x,'o-',label='linear') #x, y, "格式控制字符串",名称plt.plot(x,x**2,'x-',label='quadratic') plt.legend(loc='best') #加图例plt.xlabel('input') #x轴plt.ylabel('output') #y轴plt.show() #显示
直方图from matplotlib import pyplot as pltimport pandas as pdimport numpy as npimport seabornsamples = np.random.normal(loc=1.0, scale=0.5, size=1000)#数据源是一个随机的正态分布。loc:均值(正态分布中心),scale:正态分布的标准差,对应分布的宽度,size:输出的值赋在shape里,默认为Noneprint(samples.shape)print(samples.dtype)#数据的类型print(samples[:10])#输出一下数据集的前30个数据看看就知道生成的数据的样子了plt.hist(samples, bins=50)#绘制直方图plt.hist。plt.hist(数据源,bins=统计的区间分布,range: tuple, 显示的区间)plt.show()
## 同一图中显示两个直方图
samples_1 = np.random.normal(loc=1, scale=.5, size=10000)samples_2 = np.random.standard_t(df=10, size=10000) #返回一个具有自由度的标准T分布随机样本#np.random.standard_t(df=自由度>0,size=大小)bins = np.linspace(-3, 3, 50)plt.hist(samples_1, bins=bins, Alpha=0.5, label='samples 1')plt.hist(samples_2, bins=bins, Alpha=0.5, label='samples 2')#Alpha相当于透明度plt.legend(loc='upper left');plt.show()
散点图samples_1 = np.random.normal(loc=1, scale=.5, size=10000)samples_2 = np.random.standard_t(df=10, size=10000)bins = np.linspace(-3, 3, 50)plt.scatter(samples_1, samples_2, Alpha=0.1)#绘制散点图plt.scatter(x,y)plt.show()
import matplotlib.pyplot as pltplt.style.use('seaborn-whitegrID')import numpy as nprng = np.random.RandomState(0)for marker in ['o', '.', ',', 'x', '+', 'v', '^', '<', '>', 's', 'd']: plt.plot(rng.rand(5), rng.rand(5), marker, label="marker='{0}'".format(marker))plt.legend()plt.xlim(0, 1.8)#plt.xlim(x轴最小值,x轴最大值)plt.show()
总结 以上是内存溢出为你收集整理的Python matplotlib实践中学习(一)全部内容,希望文章能够帮你解决Python matplotlib实践中学习(一)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)