首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Python语法学习记录(16):matplotlib入门使用

  • 25-03-03 18:03
  • 4192
  • 7139
blog.csdn.net

文章目录

  • 1、简单常用的画图
  • 2、使用colormap画图
  • 3、绘制3D图
  • 0、参考文献需要补充内容

1、简单常用的画图

import numpy as np
import matplotlib.pyplot as plt

# 设置一块可视化图像
# DPI  每英寸长度内的像素点数  figsize  长宽英寸长度   像素点计算 = figsize*dpi
fig = plt.figure(dpi=128,figsize=(5,10))
# 设置子图  对之前的可视化对象 可以返回对象之后会相应的子图进行操作可以不返回对象默认最近的一块子图
# 举例中第一块子图不返回使用默认对象   第二块子图返回
fig.add_subplot(211)#211表示将绘画框划分为2行1列,最后的2表示第二幅图
t = np.arange(0, 20, 1)# 设置数据
#隐藏坐标轴 在单图上可以使用  多图上使用不太好用
# plt.axes().get_xaxis().set_visible(False)
# plt.axes().get_yaxis().set_visible(False)
# 绘制折线图
# 前面是横坐标后面是纵坐标   
# 设置线型第一个标记为颜色第二个点标记第三个为线
# 'ro-','g.-','b*-','y>-','m<-','kx-','c^-','k+-'
plt.plot(t,t,'k+-')
# 可以同时画很多个图像
plt.plot(t,t**2,color='green', marker='o', linestyle='dashed', linewidth=2, markersize=3)
# 绘制带原始数据的点图  zorder是显示顺序
plt.plot(t, t**2.1,c='red',zorder=1)
# zorder=2是为了让绘制的点盖在直线上面  
# scatter 是画散点图 
plt.scatter(t, t**2.1,c='black',marker='o',zorder=2)
# 添加标题
plt.title('test')
# 加网格看起来好看些   '-'全实线
plt.grid(linestyle='-.') 
#[]里的4个参数分别表示X轴起始点,X轴结束点,Y轴起始点,Y轴结束点
plt.axis([0,25,0,500])
# 图例
label = ['t', 't**2', 't**2.1']
# 设置坐标轴的位置
# 'best','upper right','upper left','lower left','lower right'
# 'right','center left','center right','lower center','upper center','center
plt.legend(label, loc='best')

# 设置第二块子图
p1=fig.add_subplot(212)
# 设置数据
x=[1,2,3,4,5,6,7,8]
y=[2,1,3,5,2,6,12,7]
p1.plot(x,y)
a=[1,2]
b=[2,4]
p1.scatter(a,b)
# 关闭图片   如果figure开多了也可以用这个关闭 这里如果关闭的话保存的图片就是空的了
# plt.close(fig)
# 图像保存
plt.savefig('./test.jpg')
# 展示绘画框
# plt.show()
# 显示图片后暂停两秒
# plt.pause(2)
# plt.close('all')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

2、使用colormap画图

from random import choice
import matplotlib.pyplot as plt

x_start = [0]
y_start = [0]

#生成坐标矩阵
while len(x_start)<10000:

    x_direction=choice([-1,1])
    x_distance=choice([0,1,2,3,4])
    x_step=x_direction*x_distance

    y_direction=choice([-1,1])
    y_distance=choice([0,1,2,3,4])
    y_step=y_direction*y_distance

    next_x=x_start[-1]+x_step
    next_y=y_start[-1]+y_step

    x_start.append(next_x)
    y_start.append(next_y)

#设置窗口大小,像素
plt.figure(dpi=128,figsize=(5,3))
#绘制
plt.scatter(x_start,y_start,c=list(range(10000)),cmap=plt.cm.Blues,edgecolors='none',s=15)
#突出起始与结束
plt.scatter(0,0,c='red',edgecolors='none',s=80)
plt.scatter(x_start[-1],y_start[-1],c='orange',edgecolors='none',s=80)#edgecolors='none'去除点的黑色轮廓,只显示蓝色的点
#隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.title('random—choice')
plt.savefig('./test.jpg')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

3、绘制3D图

#导入pyplot包,并简写为plt
import matplotlib.pyplot as plt

#导入3D包
from mpl_toolkits.mplot3d import Axes3D

#将绘画框进行对象化
fig = plt.figure()

#将绘画框划分为1个子图,并指定为3D图
ax = fig.add_subplot(111, projection='3d')

#定义X,Y,Z三个坐标轴的数据集
X = [1, 1, 2, 2]
Y = [3, 4, 4, 3]
Z = [1, 100, 1, 1]

#用函数填满4个点组成的三角形空间
ax.plot_trisurf(X, Y, Z)
plt.savefig('./test.jpg')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

0、参考文献需要补充内容

Matplotlib绘图–添加标注

matplotlib命令与格式:图例legend语法及设置

莫烦

Python——plt.scatter各参数详解

Python之matplotlib入门教程

使用Python绘制图表

15张图入门Matplotlib

Matplotlib实践系列:折线图完全示例

Matplotlib 快速入门

知乎matplotlib

github 教程

matplotlib官网

【matplotlib】 之 清理、清除 axes 和 figure (plt.cla、plt.clf、plt.close)

【matplotlib】 图解pyplot figure、subplot、axes、axis的区别

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树绘图库MatplotlibMatplotlib快速入门416632 人正在系统学习中
注:本文转载自blog.csdn.net的呆呆象呆呆的文章"https://blog.csdn.net/qq_41554005/article/details/103251811"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top