[英]3D Plot with a colormap - Python
I am trying to make a 3D surface plot showing voltage
vs. temp
vs. power
, and to scale the colour of each point to another array of values, known as bifurWidth
. 我正在尝试制作一个3D表面图,以显示
voltage
, temp
和power
,并将每个点的颜色缩放到另一个值数组,称为bifurWidth
。
Below is my code. 下面是我的代码。
Problems with my code: 我的代码有问题:
numpy.ndarray
generated using loadtxt
. numpy.ndarray
使用产生loadtxt
。 The error in this case is Color array must be two-dimensional
. Color array must be two-dimensional
。 However it does run if I generate a dummy set of bifurWidth values using np.arange()
. np.arange()
Why does this happen when both are numpy.ndarray
s? numpy.ndarray
时,为什么会发生这种情况? Any ideas? 有任何想法吗?
Here's my code: 这是我的代码:
from glob import glob
from pylab import *
import numpy as np
from matplotlib import cm
import os
fs = 22
# Import data.
voltage = np.loadtxt('NonLorentzianData.txt',usecols=[0])
power = np.loadtxt('NonLorentzianData.txt',usecols=[1])
bifurWidth = arange(len(voltage))#np.loadtxt('LorentzianData.txt',usecols=[3])
temp = np.loadtxt('NonLorentzianData.txt',usecols=[4])
path = np.loadtxt('NonLorentzianData.txt',usecols=[5],dtype='S16')
c = np.abs(bifurWidth)
#Plot a 3D pot showing Temperature/Voltage/Power and intensity of colour showing bifurcation size.
fig = figure()
ax = fig.add_subplot(111, projection='3d')
cmhot = get_cmap("hot")
ax.scatter(voltage,temp,power,bifurWidth,s=35,c=c,cmap=cmhot)
ax.set_ylabel('Temperature (mK)',fontsize=fs)
ax.set_xlabel('Voltage (V)',fontsize=fs)
ax.set_zlabel('Power (dB)',fontsize=fs)
ax.set_title('Locating bifurcations.',fontsize=fs)
fig.tight_layout()
fig.set_size_inches(25,15)
fig.savefig('TEST.PNG',dpi=300)
First of all, you say you want to make a surface plot, but in your code you make a scatter plot. 首先,您说要绘制表面图,但是在代码中创建散点图。 Based on the rest of your problem description I assume you want to make a scatter plot.
根据问题描述的其余部分,我假设您要绘制散点图。
When I look at the documentation of the scatterplot
function I see the following definition: 当我查看
scatterplot
函数的文档时 ,我看到以下定义:
Axes3D.scatter(xs, ys, zs=0, zdir=u'z', s=20, c=u'b', depthshade=True, *args, **kwargs)
You call it as: 您称其为:
ax.scatter(voltage,temp,power,bifurWidth,s=35,c=c,cmap=cmhot)
This means that the fourth formal parameter, zdir
, will be called with the value of bifurWidth
. 这意味着,第四个正式的参数,
zdir
,将具有的价值称为bifurWidth
。 This is not what you want, you want to use the default value of zdir
and to use the absolute value of bifurWidth
as the color (the second thing you already accomplish by setting c=c
). 这是不是你想要的,你要使用的默认值
zdir
和使用的绝对值bifurWidth
的颜色(你已经设置完成第二件事c=c
)。
Therefore, just remove the bifurWidth
parameter like so: 因此,只需删除
bifurWidth
参数,如下所示:
ax.scatter(voltage,temp,power,s=35,c=c,cmap=cmhot)
This should get rid of the error. 这应该摆脱错误。
According to the answer to this question 3D scatter plots won't work with legend so you need to make a dummy plot that isn't displayed to create the legend. 根据此问题的答案,3D散点图不适用于图例,因此您需要制作一个未显示的虚拟图来创建图例。 Here's an example related based on your question that adds a legend to a 3d scatter plot:
这是一个根据您的问题相关的示例,为3d散点图添加了图例:
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
import numpy as np
from matplotlib import cm
# Fake data
(voltage, temp) = np.meshgrid(range(10), range(10))
power1 = np.random.rand(10,10)
power2 = np.random.rand(10,10)
bifurWidth1 = 100*np.random.rand(10.,10.)
bifurWidth2 = np.random.rand(10.,10.)
# Plot data
fig = figure()
ax = fig.add_subplot(111, projection='3d')
cm1 = get_cmap("Blues")
cm2 = get_cmap("Reds")
ax.scatter(voltage, temp, power1, c = bifurWidth1, s=35, marker = 'o', cmap = cm1)
ax.scatter(voltage, temp, power2, c = bifurWidth2, s=35, marker = "^", cmap = cm2)
# Make legend
scatter1_proxy = matplotlib.lines.Line2D([0],[0], linestyle="none", c=cm1(128), marker = 'o')
scatter2_proxy = matplotlib.lines.Line2D([0],[0], linestyle="none", c=cm2(128), marker = '^')
ax.legend([scatter1_proxy, scatter2_proxy], ['label1', 'label2'], numpoints = 1)
# Label axes
fs = 12
ax.set_ylabel('Temperature (mK)',fontsize=fs)
ax.set_xlabel('Voltage (V)',fontsize=fs)
ax.set_zlabel('Power (dB)',fontsize=fs)
ax.set_title('Locating bifurcations.',fontsize=fs)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.