iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >【python图像处理】python绘制
  • 325
分享到

【python图像处理】python绘制

图像处理python 2023-01-31 01:01:13 325人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

3D图形在数据分析、数据建模、图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行3D图形的绘制,包括3D散点、3D表面、3D轮廓、3D直线(曲线)以及3D文字等的绘制。 准备工作: Python中绘制3

3D图形在数据分析、数据建模、图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行3D图形的绘制,包括3D散点、3D表面、3D轮廓、3D直线(曲线)以及3D文字等的绘制。


准备工作:

Python中绘制3D图形,依旧使用常用的绘图模块matplotlib,但需要安装mpl_toolkits工具包,安装方法如下:windows命令行进入到python安装目录下的Scripts文件夹下,执行: pip install --upgrade matplotlib即可;linux环境下直接执行该命令。

安装好这个模块后,即可调用mpl_tookits下的mplot3d类进行3D图形的绘制。

下面以实例进行说明。


1、3D表面形状的绘制

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

# Plot the surface
ax.plot_surface(x, y, z, color='b')

plt.show()

这段代码是绘制一个3D的椭球表面,结果如下:



2、3D直线(曲线)的绘制

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.GCa(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()

这段代码用于绘制一个螺旋状3D曲线,结果如下:



3、绘制3D轮廓

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)

plt.show()

绘制结果如下:



4、绘制3D直方图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])

# Construct arrays for the anchor positions of the 16 bars.
# Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,
# ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid
# with indexing='ij'.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)

# Construct arrays with the dimensions for the 16 bars.
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')

plt.show()

绘制结果如下:


5、绘制3D网状线

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()

绘制结果如下:



6、绘制3D三角面片图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np


n_radii = 8
n_angles = 36

# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)

# Repeat all angles for each radius.
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)

# Convert polar (radii, angles) coords to cartesian (x, y) coords.
# (0, 0) is manually added at this stage,  so there will be no duplicate
# points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())

# Compute z to make the pringle surface.
z = np.sin(-x*y)

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

plt.show()

绘制结果如下:



7、绘制3D散点图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np


def randrange(n, vmin, vmax):
    '''
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed UnifORM(vmin, vmax).
    '''
    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

绘制结果如下:



8、绘制3D文字

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.gca(projection='3d')

# Demo 1: zdir
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (1, 4, 4, 9, 4, 1)
ys = (2, 5, 8, 10, 1, 2)
zs = (10, 3, 8, 9, 1, 8)

for zdir, x, y, z in zip(zdirs, xs, ys, zs):
    label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
    ax.text(x, y, z, label, zdir)

# Demo 2: color
ax.text(9, 0, 0, "red", color='red')

# Demo 3: text2D
# Placement 0, 0 would be the bottom left, 1, 1 would be the top right.
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)

# Tweaking display region and labels
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()

绘制结果如下:



9、3D条状图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.arange(20)
    ys = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,
    # the first bar of each set will be colored cyan.
    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

绘制结果如下:




2017.09.21

--结束END--

本文标题: 【python图像处理】python绘制

本文链接: https://www.lsjlt.com/news/184658.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【python图像处理】python绘制
    3D图形在数据分析、数据建模、图形和图像处理等领域中都有着广泛的应用,下面将给大家介绍一下如何使用python进行3D图形的绘制,包括3D散点、3D表面、3D轮廓、3D直线(曲线)以及3D文字等的绘制。 准备工作: python中绘制3...
    99+
    2023-01-31
    图像处理 python
  • python数字图像处理图像的绘制详解
    目录正文一、用figure函数和subplot函数分别创建主窗口与子图二、用subplots来创建显示窗口与划分子图三、其它方法绘图并显示正文 实际上前面我们就已经用到了图像的绘制,...
    99+
    2024-04-02
  • python数字图像处理之基本图形的绘制
    目录引言1、画线条2、画圆3、多边形4、椭圆5、贝塞儿曲线6、画空心圆7、空心椭圆引言 图形包括线条、圆形、椭圆形、多边形等。 在skimage包中,绘制图形用的是draw模块,不要...
    99+
    2024-04-02
  • Python图像处理之图像融合与ROI区域绘制详解
    目录一.图像融合二.图像ROI区域定位三.图像属性(1)shape(2)size(3)dtype四.图像通道分离及合并(1)split()函数(2)merge()函数五.图像类型转换...
    99+
    2024-04-02
  • Python pygame绘制游戏图像
    目录前言1. 理解图像并实现图像绘制2. 代码演练-绘制背景图像3. 代码演练-绘制英雄图像前言 本节,我们将使用pygame模块完成飞机大战游戏的实战开发,飞机大战游戏的简要概括如...
    99+
    2024-04-02
  • Python图像处理【3】Python图像处理库应用
    Python图像处理库应用 0. 前言1. 将 RGB 图像转换为灰度图像算法1.1 算法原理3.2 算法实现 2. 使用 PIL 库计算图像差异2.1 算法原理2.2 算法实现 ...
    99+
    2023-09-06
    python 图像处理 计算机视觉
  • Python+OpenCV绘制多instance的Mask图像
    目标:Mask中,不同值表示不同的实例(instance),在原图中,绘制不同的instance实例,每个实例用不同颜色表示,实例边界用白色表示。 源码: def generate_...
    99+
    2024-04-02
  • Python利用Turtle绘制虎年图像
    目录导语一、代码展示二、效果展示导语 2022年是农历壬寅虎年,在自然界中,虎有“百兽之王”之称 它的王者之风与勇猛,被作为威仪和权势的象征,千百年来,人们崇...
    99+
    2024-04-02
  • 怎么用Python的Pyecharts绘制图像
    本篇内容介绍了“怎么用Python的Pyecharts绘制图像”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!前言:Echarts 是百度开源...
    99+
    2023-06-29
  • python如何绘制三维函数图像图
    在python中使用matplotlib库绘制三维函数图像图,具体方法如下:import matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npi...
    99+
    2024-04-02
  • JavaOpenCV图像处理之图形与文字绘制
    目录前言核心代码效果图前言 代码地址 序號名稱方法1圖像 添加文字Imgproc.putText2圖像 畫直綫Imgproc.line3圖像 畫橢圓Imgproc.ellipse4圖...
    99+
    2024-04-02
  • Python怎么用PIL图像处理库绘制国际象棋棋盘
    本篇内容介绍了“Python怎么用PIL图像处理库绘制国际象棋棋盘”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!目录1 PIL绘制国际象棋棋...
    99+
    2023-06-20
  • 【python图像处理】python中定
    python中的颜色相关的定义在matplotlib模块中,为方便使用,这里给大家展示一下在这个模块中都定义了哪些选颜色。 1、颜色名称的导出 导出代码如下: import matplotlib for name, hex in matp...
    99+
    2023-01-31
    图像处理 python
  • python skimage图像处理
    目录引言scikit-image进行数字图像处理图片信息skimage包的子模块从外部读取图片并显示程序自带图片保存图片图像像素的访问与裁剪color模块的rgb2gray()函数结...
    99+
    2024-04-02
  • 用Python绘制一个仿黑洞图像
    目录简介单位制观测绘图简介 黑洞图像大家都知道,毕竟前几年刚发布的时候曾火遍全网,甚至都做成表情包了。 问题在于,凭什么认为这就是黑洞的照片,而不是一个甜甜圈啥的给整模糊了得到的呢...
    99+
    2023-02-24
    Python绘制仿黑洞图像 Python仿黑洞 Python黑洞
  • Python+Matplotlib怎么绘制双y轴图像
    今天小编给大家分享一下Python+Matplotlib怎么绘制双y轴图像的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。双Y...
    99+
    2023-06-30
  • 怎么使用python进行图像绘制
    本文小编为大家详细介绍“怎么使用python进行图像绘制”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用python进行图像绘制”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。正文实际上前面我们就已经用到了...
    99+
    2023-07-02
  • Python图像处理之图像拼接
    目录一、前言二、特征点匹配三、匹配错误的特征点干扰四、消除干扰五、RANSAC进行图像匹配六、总结一、前言 图像拼接技术就是将数张有重叠部分的图像(可能是不同时间、不同视角或者不同传...
    99+
    2024-04-02
  • python 绘制3D图
    python 绘制3D图 1.散点图代码输入的数据格式 2.三维表面 surface代码输入的数据格式scatter + surface图形展示 3. 三维瀑布图waterfall代码...
    99+
    2023-09-25
    python 机器学习 matplotlib
  • Python图像处理之图像量化处理详解
    目录一.图像量化处理原理二.图像量化实现三.图像量化等级对比四.K-Means聚类实现量化处理五.总结一.图像量化处理原理 量化(Quantization)旨在将图像像素点对应亮度的...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作