0

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.

Below is my code.

Problems with my code:

  1. I cannot make my code run for a 'bifurWidth' numpy.ndarray generated using loadtxt. The error in this case is Color array must be two-dimensional. However it does run if I generate a dummy set of bifurWidth values using np.arange(). Why does this happen when both are numpy.ndarrays?
  2. I have no idea how to get this working, with a legend visible, for a 3D surface plot.

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)

2 Answers 2

1

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:

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. 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).

Therefore, just remove the bifurWidth parameter like so:

ax.scatter(voltage,temp,power,s=35,c=c,cmap=cmhot)

This should get rid of the error.

Sign up to request clarification or add additional context in comments.

Comments

1

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. Here's an example related based on your question that adds a legend to a 3d scatter plot:

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()

3d scatter plot with legend

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.