文章目录

  • 用bar绘制垂直条形图
  • 用hist绘制柱状图
  • plot曲线图
  • scatter绘制散点图
  • bar绘制垂直条形图和barh绘制水平条形图
  • fill和fill_between绘制填充图和stackplot绘堆叠图
  • hist绘制柱状图和boxplot绘制箱线图
  • imshow显示二维图形
  • contour+contourf绘制等高线
  • Axes3D绘制三维曲线图
  • subplot绘制多图
  • figure绘制多图
  • figure图的嵌套
  • 主次坐标轴

用bar绘制垂直条形图

  • 垂直条形图
x=[5,8,10]
y=[12,16,6]
std = [5,6,7]
x2=[6,9,11]
y2=[6,15,7]
std2 = [1,2,3]
plt.bar(x,y,yerr=std )
plt.bar(x2,y2,yerr=std2)
plt.ylabel("Y axis")
plt.xlabel("X axis")for i, j in zip(x, y):plt.text(i, j+1, '%d'%j)for i, j in zip(x2, y2):plt.text(i, j+1, '%d'%j)plt.show()

n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')for x,y in zip(X,Y1):plt.text(x, y+0.05, '%.2f' % y) # 对应位置显示大小plt.ylim(-1.25,+1.25)
plt.show()

  • 堆叠条形图
x=[5,8,10]
y=[12,16,6]
std = [5,6,7]y2=[6,15,7]
std2 = [1,2,3]
plt.bar(x,y,yerr=std)
plt.bar(x,y2,bottom=y,yerr=std2) # 以y中的值为底
plt.ylabel("Y axis")
plt.xlabel("X axis")
plt.legend(('y','y2'))
plt.show()

用hist绘制柱状图

a=np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
np.histogram(a,bins = [0,20,40,60,80,100])
hist,bins = np.histogram(a,bins=[0,20,40,60,80,100])
print(hist)
print(bins)
plt.hist(a,bins=[0,20,40,60,80,100])  # bins表示分段,此例中分成5段
plt.title("histogram")
plt.show()

plot曲线图

x = np.linspace(0,10,100)
fig=plt.figure(figsize=(10,4))
ax0 = fig.add_subplot(1,3,1)
ax1 = fig.add_subplot(1,3,2)
ax2 = fig.add_subplot(1,3,3)for i in range(1,6):ax0.plot(x,i*x)
for i,ls in enumerate(['-','--',':','-.']):ax1.plot(x,np.cos(x)+i, linestyle=ls)
for i, (ls,mk) in enumerate(zip(['','-',':'],['o','^','s'])):ax2.plot(x,np.cos(x)+i*x, linestyle=ls,marker=mk,markevery=10)plt.show()

scatter绘制散点图

x,y,z = np.random.normal(0,1,(3,100))
t = np.arctan2(y,x)
size = 50*np.cos(2*t)**2+10fig, axes = plt.subplots(1,3,figsize = (10,4))axes[0].scatter(x,y,marker='o',facecolor='black',s=80)
axes[1].scatter(x,y,s=size,marker='s',color='darkblue') # marker的大小随参数s的大小变化
axes[2].scatter(x,y,c=z,s=size,cmap='gist_ncar') # marker的大小和颜色随参数s和参数c的大小变化plt.show()

bar绘制垂直条形图和barh绘制水平条形图

y=[1,3,4,5.5,3,2]
err = [0.2,1,2.5,1,1,0.5]
x = np.arange(len(y))
fig,axes = plt.subplots(1,3,figsize=(10,4))
axes[0].bar(x,y,yerr=err,color='lightblue',ecolor='black')
axes[0].set_ylim([0,10])
axes[0].set_title("bar")y = np.arange(8)
x1 = y+np.random.random(8)+1
x2 = y+3*np.random.random(8)+1
axes[1].barh(y,x1,color='lightblue')
axes[1].barh(y,-x2,color='salmon')
axes[1].set_title("barh")left = np.random.randint(0,10,10)
bottom = np.random.randint(0,10,10)
width = np.random.random(10)+0.5
height = np.random.random(10)+0.5
axes[2].bar(left,height, width,bottom,color='salmon')plt.show()

fill和fill_between绘制填充图和stackplot绘堆叠图

def stackplot_data():x = np.linspace(0, 10, 100)y = np.random.normal(0, 1, (5, 100))y = y.cumsum(axis=1)y -= y.min(axis=0, keepdims=True)return x, ydef sin_data():x = np.linspace(0, 10, 100)y = np.sin(x)y2 = np.cos(x)return x, y, y2def fill_data():t = np.linspace(0, 2*np.pi, 100)r = np.random.normal(0, 1, 100).cumsum()r -= r.min()return r * np.cos(t), r * np.sin(t)fig, axes = plt.subplots(1,3,figsize=(10,4))
x, y = fill_data()
axes[0].fill(x,y,color='lightblue')
axes[0].set_title("fill")x, y1, y2 = sin_data()
err = np.random.rand(x.size)**2+0.1
y = 0.7*x+2
axes[1].fill_between(x,y1,y2,where=y1>y2,color='lightblue')  # y1>y2的区域用颜色lightblue填充
axes[1].fill_between(x,y1,y2,where=y1<y2,color='forestgreen')# y1<y2的区域用颜色forestgreen填充x, y = stackplot_data()
axes[2].stackplot(x, y.cumsum(axis=0), alpha=0.5)

hist绘制柱状图和boxplot绘制箱线图

def generate_data():means = [0, -1, 2.5, 4.3, -3.6]sigmas = [1.2, 5, 3, 1.5, 2]# Each distribution has a different number of samples.nums = [150, 1000, 100, 200, 500]dists = [np.random.normal(*args) for args in zip(means, sigmas, nums)]return distsfig, axes = plt.subplots(1,3,figsize=(10,4))
colors = ['cyan','red','blue','green','purple']
dists = generate_data()
np.array(dists).shape
axes[0].set_color_cycle(colors)
for dist in dists:axes[0].hist(dist,bins=20,density=True,edgecolor='none',alpha=0.5)result = axes[1].boxplot(dists, patch_artist=True,notch=True,vert=False)
for box, color in zip(result['boxes'],colors):box.set(facecolor=color, alpha=0.5)
for item in ['whiskers','caps','medians']:plt.setp(result[item],color='gray',linewidth=1.5)
plt.setp(result['fliers'], markeredgecolor='gray', markeredgewidth=1.5)
plt.setp(result['medians'], color='black')
result = axes[2].violinplot(dists, vert=False, showmedians=True)
for body, color in zip(result['bodies'], colors):body.set(facecolor=color, alpha=0.5)
for item in ['cbars', 'cmaxes', 'cmins', 'cmedians']:plt.setp(result[item], edgecolor='gray', linewidth=1.5)
plt.setp(result['cmedians'], edgecolor='black')
plt.show()

imshow显示二维图形

  • imshow显示二维图形
vegetables = ["cucumber", "tomato", "lettuce", "asparagus","potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening","Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])fig, ax = plt.subplots(figsize=(10,8))
im = ax.imshow(harvest)ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(vegetables)))ax.set_xticklabels(farmers)
ax.set_yticklabels(vegetables)
for i in range(len(vegetables)):for j in range(len(farmers)):text = ax.text(j, i, harvest[i, j],ha="center", va="center", color="w")
plt.tight_layout() ## 自动调整图形大小
plt.show()

  • imshow显示图形+colorbar
data = np.random.random((256,256))
fig,ax = plt.subplots()
im = ax.imshow(data,cmap='seismic')
fig.colorbar(im)
plt.show()

contour+contourf绘制等高线

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)# 第4个参数用来确定轮廓线\区域的数量和位置,数字4表示使用4个数据间隔;绘制5条轮廓线
plt.contourf(X,Y,f(X,Y),4,alpha=0.75,cmap=plt.cm.hot)  # 用于填充轮廓,不会绘制轮廓线
C = plt.contour(X,Y,f(X, Y),4,color='black',linewidth=0.5)  # 用于绘制轮廓线
plt.clabel(C,inline=True,fontsize=20)  # 给contour的返回对象增加标签到轮廓线上
# 去除横纵坐标
plt.xticks(())
plt.yticks(())
plt.show()

Axes3D绘制三维曲线图

from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()
ax = Axes3D(fig)n=256
x=np.arange(-4,4,0.25)
y=np.arange(-4,4,0.25)X,Y = np.meshgrid(x,y)
R = np.sqrt(X**2+Y**2)
Z = np.sin(R)ax.plot_surface(X,Y,Z,cmap=plt.get_cmap('rainbow'))
ax.contour(X,Y,Z,offset=-2,cmap='rainbow') # 绘制轮廓线
ax.set_zlim(-2,2)
plt.show()

subplot绘制多图

plt.figure()plt.subplot(2,1,1)  # 第1行只有1列
plt.plot([0,1],[0,1])plt.subplot(2,3,4)
plt.plot([0,1],[0,1])plt.subplot(2,3,5)
plt.plot([0,1],[0,1])plt.subplot(2,3,6)
plt.plot([0,1],[0,1])
plt.show()

figure绘制多图

plt.figure()# (0,0)表示从第1行第1列开始画图,整个图形占1行3列
ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)
plt.plot([0,1],[0,1])# (1,0)表示从第2行第1列开始画图,整个图形占1行2列
ax2 = plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1)
plt.plot([0,1],[0,1])# (1,2)表示从第2行第3列开始画图,整个图形占1行1列
ax3 = plt.subplot2grid((3,3),(1,2),colspan=1,rowspan=1)
plt.plot([0,1],[0,1])# (2,0)表示从第3行第1列开始画图,整个图形占1行3列
ax4 = plt.subplot2grid((3,3),(2,0),colspan=3,rowspan=1)
plt.plot([0,1],[0,1])
plt.show()

figure图的嵌套

fig = plt.figure() # 整个图形x = [1,2,3,4,5,6,7]
y = [1,3,4,2,5,8,6]# figure的百分比,从figure距左边10%长度,距下边10%长度的位置开始绘制,宽高是figure宽高的80%
left, bottom, width, height= 0.1, 0.1, 0.8, 0.8# 获得绘制的句柄
ax1 = fig.add_axes([left,bottom,width,height])
# 获得绘制的句柄
ax1.plot(x,y,'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('test')# 嵌套方法一
# figure的百分比,从figure距左边20%长度,距下边60%长度的位置开始绘制,宽高是figure宽高的25%
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
# 获得绘制的句柄
ax2 = fig.add_axes([left,bottom,width,height])
# 绘制点(x,y)
ax2.plot(x,y,'r')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('part1')# 嵌套方法二
left, bottom, width, height = 0.6, 0.2, 0.25, 0.25
plt.axes([left, bottom,width,height])
plt.plot(x,y,'r')
plt.xlabel('x')
plt.ylabel('y')
plt.title('part2')plt.show()

主次坐标轴

x = np.arange(0,10,0.1)
y1 = 0.05*x**2
y2 = -1 * y1fig, ax1 = plt.subplots()# 得到ax1的对称轴ax2
ax2 = ax1.twinx()#绘制图像
ax1.plot(x,y1,'g-')
ax2.plot(x,y2,'b--')#设置label
ax1.set_xlabel('X data')
ax1.set_ylabel('Y1',color='g')
ax2.set_ylabel('Y2',color='b')plt.show()

【Python数据科学】Matplotlib画图相关推荐

  1. Python数据科学库02(matplotlib)

    Python数据科学库02 学习02 matplotlib 对比常用统计图 折线图的更多应用场景: 1.呈现公司产品(不同区域)每天活跃用户数 2.呈现app每天下载数量 3.呈现产品新功能上线后,用 ...

  2. 【Python数据科学快速入门系列 | 06】Matplotlib数据可视化基础入门(一)

    这是机器未来的第52篇文章 原文首发地址:https://robotsfutures.blog.csdn.net/article/details/126899226 <Python数据科学快速入 ...

  3. python书籍推荐:Python数据科学手册

    所属网站分类: 资源下载 > python电子书 作者:today 链接:http://www.pythonheidong.com/blog/article/448/ 来源:python黑洞网 ...

  4. Python数据科学手册

    Python数据科学手册 下载地址 https://pan.baidu.com/s/13fCOKgWv0sl9h1MnT-X-Yw 扫码下面二维码关注公众号回复 100093获取分享码 本书目录结构如 ...

  5. python数据科学用法_Matplotlib 使用 - 《Python 数据科学手册》学习笔记

    一.引入 import matplotlib as mpl import matplotlib.pyplot as plt 二.配置 1.画图接口 Matplotlib 有两种画图接口: (1)一个是 ...

  6. python数据科学手册_小白入门Python数据科学

    前言 本文讲解了从零开始学习Python数据科学的全过程,涵盖各种工具和方法 你将会学习到如何使用python做基本的数据分析 你还可以了解机器学习算法的原理和使用 说明 先说一段题外话.我是一名数据 ...

  7. 的确好用!Python数据科学速查表中文版强势来袭!

    1速查速记,不二之选! DataCamp 推出的 Python 数据科学速查表(中文版),一共 11 张表,包括:Python 基础.导入数据.Jupyter Notebook.Numpy 基础.Pa ...

  8. python数据科学-多变量数据分析

    总第87篇 01|写在前面: 在前面我们研究了单列(变量)数据情况,现实中的案例大多都是多列(变量)的,即影响一件事情的因素有多个,我们除了要看单列数据以外还需要看看这不同列之间是否存在某些联系.常见 ...

  9. python数据科学-单变量数据分析

    总第85篇 01|背景: 我们在做机器学习之前,需要自己先对数据进行深入的了解(这些数据是什么类型,总共有多少数据,有没有缺失值,均值是多少之类的),只有自己对数据足够了解了,才能够更好地利用机器学习 ...

  10. python 数据科学书籍_您必须在2020年阅读的数据科学书籍

    python 数据科学书籍 "We're entering a new world in which data may be more important than software.&qu ...

最新文章

  1. python tkinter选择路径控件_Python3 Tkinter选择路径功能的实现方法
  2. [五] JavaIO之InputStream OutputStream简介 方法列表说明
  3. CV之ICG:计算机视觉之图像标题生成(Image Caption Generator)算法的简介、使用方法、案例应用之详细攻略
  4. LVS负载均衡下session共享的实现方式-持久化连接
  5. 设计模式——工厂模式(二)
  6. 加载elementor时出现问题_不锈钢管在焊接时出现问题要怎么解决?
  7. 由对称性知定点一定在x轴上_线上优秀教学案例(九)|计算机科学与工程学院刘钊:“延期不延教”之“1+X课堂”...
  8. 【python】python 爬虫(python抓取网站的图片)
  9. svg如何平铺 html5,如何在HTML5中使用SVG
  10. 深度学习:循环神经网络RNN
  11. SQL Server中的事务日志管理(2/9):事务日志架构概述
  12. windows 7 安装 db2 v11.1 下载 安装教程
  13. 郑州调频广播频率表 转载
  14. 如何防止Tensorflow分配整个GPU内存?
  15. 人工智能中,自动驾驶汽车是如何自动识别交通标志的?
  16. 实验吧天网管理系统Writeup
  17. 计算机软件设计硕士生导师,孙明副教授、硕士生导师-山东大学软件与数据工程研究中心...
  18. matlab 浮点数有效位,matlab浮点数精度
  19. 教育项目之讲师模块介绍
  20. Python爬虫成长之路:抓取证券之星的股票数据(转)

热门文章

  1. FRAM作为代码存储器应用中的单芯片解决方案
  2. 联想服务器接显示器无显示,win10系统联想笔记本接显示器不显示的解决方法
  3. 氙灯老化测试研究|汽车氙灯测试方法:相关性研究
  4. android 电话号码标记,Android
  5. Oracle 10g New Features - AWR - Basical Conception
  6. android studio ndk 引入系统头文件时出现红色波浪线
  7. 目标检测YOLOv3论文详解
  8. CGAL-三角化的数据结构(tds_2)
  9. Linux操作系统——批量创建用户
  10. java计算机毕业设计网络游戏管理网站源码+数据库+系统+lw文档+mybatis+运行部署