ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python matplotlib实践中学习(一)

2021-06-11 23:57:24  阅读:242  来源: 互联网

标签:plt Python random 实践 matplotlib samples np import bins


基本图形

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import seaborn

plt.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 plt
import pandas as pd
import numpy as np
import seaborn

samples = np.random.normal(loc=1.0, scale=0.5, size=1000)
#数据源是一个随机的正态分布。loc:均值(正态分布中心),scale:正态分布的标准差,对应分布的宽度,size:输出的值赋在shape里,默认为None
print(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 plt
plt.style.use('seaborn-whitegrid')
import numpy as np

rng = 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()

在这里插入图片描述

标签:plt,Python,random,实践,matplotlib,samples,np,import,bins
来源: https://blog.csdn.net/m0_46344235/article/details/117828030

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有