从3D列表中绘制3D条形图 [英] plotting 3d barchart from a 3D list

查看:61
本文介绍了从3D列表中绘制3D条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,看起来像(包含约25个数据点):

I have a dataset looks like(contains ~25 data-points):

x=[2.225  2.325  2.425  2.075  2.375  1.925  1.975  1.775  1.975  2.375]  
y=[147.75  130.25  161.75  147.75  165.25  151.25  158.25  151.25  172.25  123.25]  
z=[-1.36, -0.401, -0.741, -0.623, -0.44, -0.37, 0.120, 2.8, 0.026, -1.19]  

我正在尝试使用数据绘制3D条形图.
我正在尝试:

I'm trying to plot a 3D-bar chart using the data.
I was trying like:

from mpl_toolkits.mplot3d import Axes3D         
fig_3d_bar = plt.figure(figsize=(7, 5))  
dx = fig_3d_bar.add_subplot(111, projection='3d')  

x_pos = np.array(x)  
y_pos = np.array(y)  
z_pos = np.zeros(len(x))    

dx = np.ones(len(x))  
dy = np.ones(len(y))  
dz = z   

dx.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')  

但这给我一个错误报告,为:

But this is giving me an error report as:

    dx.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')  
AttributeError: 'numpy.ndarray' object has no attribute 'bar3d'  

一点帮助将大有裨益.不知道出了什么问题.
谢谢.

A little help would serve great. Don't know what is going wrong.
thank you.

推荐答案

您的代码中有错误.您将变量名称 dx 用于 Axes 对象和条形图的大小.我想你想要

There is an error in your code. You use the variable name dx for both the Axes object and the size of the bars. I guess you want

ax = fig_3d_bar.add_subplot(111, projection='3d')
ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color='#00ceaa')   

条形尺寸

由于x和y数据的比例不同,条形图在图中显示的非常宽.您可以通过相应地缩放 dx dy 来调整它们.

dx = np.ones(len(x))*0.1
dy = np.ones(len(y))*5

条形颜色

可以使用 ScalarMappable将条形的颜色调整为z值实例.为此,您需要一个范数对象,该对象将z值缩放到[0,1]范围.您可以选择任何预定义的颜色图,也可以创建自己的颜色.

Bar Colors

The colors of the bars can be adapted to the z-values by using a ScalarMappable instance. For this you need a norm object that scales the z-values to the range [0,1]. You can choose any of the predefined colormaps or create your own.

import matplotlib.colors as cls
import matplotlib.cm as cm

norm = cls.Normalize() # Norm to map the z values to [0,1]
norm.autoscale(z)
cmap = cm.ScalarMappable(norm, 'jet') # Choose any colormap you want

ax.bar3d(x_pos, y_pos, z_pos, dx, dy, dz, color=cmap.to_rgba(z))

这篇关于从3D列表中绘制3D条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆