Python matplotlib实践中学习(一)

基本图形

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()		#显示

Python matplotlib实践中学习(一)

直方图

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

Python matplotlib实践中学习(一)## 同一图中显示两个直方图

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

Python matplotlib实践中学习(一)

散点图

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

Python matplotlib实践中学习(一)

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

Python matplotlib实践中学习(一)

上一篇:覆盖率— SV,SystemVerilog


下一篇:python 图像处理(8):直方图与均衡化