Matplotlib允许将facecolors作为参数传递给例如

ax.plot_surface.

这意味着您必须在自己的电脑上执行2D插值

当前的颜色数组,因为您当前仅在

矩形面的角(您确实提到您有一个直线

格).

给你一个简单的例子:

import numpy as np

y,x = np.mgrid[1:10:10j, 1:10:10j] # returns 2D arrays

# You have 1D arrays that would make a rectangular grid if properly reshaped.

y,x = y.ravel(), x.ravel() # so let's convert to 1D arrays

z = x*(x-y)

colors = np.cos(x**2) - np.sin(y)**2

现在,我有一个与您相似的数据集(x,y,z和

颜色).请注意,颜色是为

每个点(x,y).但是,当您要使用plot_surface进行绘制时,

生成矩形斑块,其角由那些点给出.

因此,接着进行插值:

from scipy.interpolate import RectBivariateSpline

# from scipy.interpolate import interp2d # could 've used this too, but docs suggest the faster RectBivariateSpline

# Define the points at the centers of the faces:

y_coords, x_coords = np.unique(y), np.unique(x)

y_centers, x_centers = [ arr[:-1] + np.diff(arr)/2 for arr in (y_coords, x_coords)]

# Convert back to a 2D grid, required for plot_surface:

Y = y.reshape(y_coords.size, -1)

X = x.reshape(-1, x_coords.size)

Z = z.reshape(X.shape)

C = colors.reshape(X.shape)

#Normalize the colors to fit in the range 0-1, ready for using in the colormap:

C -= C.min()

C /= C.max()

interp_func = RectBivariateSpline(x_coords, y_coords, C.T, kx=1, ky=1) # the kx, ky define the order of interpolation. Keep it simple, use linear interpolation.

在最后一步中,您可能还已经使用过interp2d(with kind =’linear’

替换kx = 1,ky = 1).但是由于文档建议使用更快

RectBivariateSpline …

现在您可以绘制它了:

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.cm as cm

fig = plt.figure()

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

r = ax.plot_surface(X,Y,Z,

facecolors=cm.hot(interp_func(x_centers, y_centers).T),

rstride=1, cstride=1) # only added because of this very limited dataset

如您所见,面上的颜色与数据集的高度无关.

请注意,您可能以为只需将2D数组C传递给facecolors就可以了,而matplotlib不会抱怨.但是,结果是不准确的,因为matplotlib将仅使用C的一部分作为面色(它似乎忽略了C的最后一列和最后一行).这等效于仅使用由整个色块中一个坐标(例如,左上角)定义的颜色.

一种更简单的方法是让matplotlib进行插值并获取facecolor,然后将其传递给实际图:

r = ax.plot_surface(X,Y,C, cmap='hot') # first plot the 2nd dataset, i.e. the colors

fc = r.get_facecolors()

ax.clear()

ax.plot_surface(X, Y, Z, facecolors=fc)

python变量图片_在Python中向3D图添加第4个变量相关推荐

  1. python 存储图片_使用python存储网页上的图片实例

    使用python存储网页上的图片实例 本文介绍在已知网络图片的地址下,存储图片到本地 本文例子随便选择LOFTER上一张图片,复制图片的地址,如下图所示 在Python中输入代码 import req ...

  2. python ppt 图片_利用Python将PPT转换为图片并合成长图

    最近因为某些需求需要将PPT转换为图片并合并成预览图,于是第一时间就想到了用python解决问题,过程中参考了一位老哥写的文章,链接如下.Python行家:用Python实现ppt转化图片(附带长图合 ...

  3. python制作图片_用python 制作图片转pdf工具

    最近因为想要看漫画,无奈下载的漫画是jpg的格式,网上的转换器还没一个好用的,于是乎就打算用python自己DIY一下: 这里主要用了reportlab.开始打算随便写几行,结果为若干坑纠结了挺久,于 ...

  4. python浮雕图片_用Python来画浮雕画

    浮雕艺术在世界各地都可以见到,中国古代在唐朝以来就有许多浮雕效果的东西,很多的大型纪念性建筑都有这种作为装饰,常见的有花窗,龙柱等. 简单的来说,浮雕就是把所要呈现的图像突起于石头表面,根据凹凸的程度 ...

  5. python笔记图片_【Python教程】雨痕 的《Python学习笔记》(附脑图)

    更多 近日,在某微博上看到有人推荐了这本作者是 雨痕 的<Python学习笔记>,从github上下载下来看了下,确实很不错. 注意,这本学习笔记不适合Python新手学习. 从目录上看, ...

  6. python绘制多边形_在python-matplotlib中绘制3D多边形

    我无法通过网络浏览以下简单问题的解决方案: 如何使用顶点值绘制3D多边形(例如,填充的矩形或三角形)?我尝试了很多想法,但都失败了,请参阅: from mpl_toolkits.mplot3d imp ...

  7. python三维图视角旋转_如何在python中旋转3d图? (或作为动画)使用鼠标旋转三维视图...

    我有这段代码,其中包含一个3D图.我在Spyder中运行代码;我想知道是否可以使这个绘图旋转(360度)并保存. 谢谢! P.s.对不起,如果这是一个愚蠢的问题,但我是Python的newby.如何在 ...

  8. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  9. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

最新文章

  1. spring mvc中的@propertysource
  2. spring resttemplate 中文参数_SpringBoot使用RestTemplate访问第三方接口
  3. Java 从业一年的心得体会
  4. java飞机大战分数累加代码_JAVA 基础编程练习题39 【程序 39 分数累加】
  5. 1107班html大赛比赛说明 同学们需注意的事项
  6. centos 7 一键安装gitlab
  7. 研究生计划 三
  8. (二十)WebGIS中图层树功能的设计和实现
  9. 开源PaaS Rainbond发布v3.7.2版本,帮助企业快速构建应用市场 1
  10. 思想的交流,扩大视野
  11. eXeScope的应用
  12. 小米 线刷 android,小米10 Android 11 Beta 1线刷包已放出,安卓11/MIUI 12二选一
  13. 阿里面试题:设计相关的系统对外提供商品实时价格获取功能
  14. 《缠中说禅108课》69:月线分段与上海大走势分析、预判
  15. 墨干V1.1.0: 新一代结构化编辑器的雏形
  16. mt4 python神经网络_用Python写MT4自动交易策略来炒外汇
  17. Mac下Sunny_Ngrok内网地址映射成外网
  18. 西安音乐学院人计算机学院,西安音乐学院.我校5部作品“中国大学生计算机设计大赛”获奖...
  19. NVT平台model屏幕配置
  20. 微信微网站的服务器ip地址查询,微信开发之(三)获取微信服务器IP地址

热门文章

  1. cocos2dx 物理碰撞
  2. 聊聊restful和restframework
  3. 细说.NET中的多线程 (四 使用锁进行同步)
  4. ios即时通讯客户端开发之-mac上搭建openfire服务器
  5. CentOS 6.8下ELK+filebeat+redis 日志分析平台
  6. ELK下Elasticsearch如何关掉服务
  7. 常见Windows硬件故障
  8. Eclipse中,Open Type(Ctrl+Shift+T)失效后做法。
  9. c# mysql datetime 判断为空 dbnull_转:SqlServer中的datetime类型的空值和c#中的DateTime的空值的...
  10. 【Oracle】RAC中控制文件多路复用