Как добавить цветовую шкалу для отображения скалярных значений на моем трехмерном графике поверхности с помощью jupyter? - PullRequest
0 голосов
/ 25 апреля 2019

Я пытался создать трехмерный поверхностный график, используя Matplotlib с Python3 через Блокнот Jupyter, чтобы визуализировать полное сопротивление цепи с 3 компонентами. У меня есть сопротивление компонента один, два и три на оси x, y, z соответственно, и я пытался визуализировать полное сопротивление, назначая цвета поверхности. Мне удалось сгенерировать поверхностный график и соответствующим образом его покрасить, но когда я пытаюсь ввести цветную полосу в график, я сталкиваюсь с большим количеством ошибок. Буду признателен за любую помощь в добавлении цветовой шкалы и надписи и ее масштабировании

import numpy as np
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import matplotlib as mpl
import math
import matplotlib.pyplot as plt
import matplotlib.colors as colors
%matplotlib inline

from mpl_toolkits import mplot3d
from matplotlib import cm

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

def f(r1,r2):
return (r1*r2)/(r1+r2)
r1 = np.linspace(0.001,15,100)
r2 = np.linspace(0.001,15,100)
X,Y = np.meshgrid(r1,r2)
W = f(X,Y)

def g(r12,r3):
return (r12*r3)/(r12+r3)
r12 = (r1*r2)/(r1+r2)
r3 = np.linspace(0.001,15,100)
A,B = np.meshgrid(r12,r3)
Z = g(A,B)

minn, maxx = Z.min(), Z.max()
norm = colors.LogNorm(minn,maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='coolwarm')
m.set_array([Z])
fcolors = cm.coolwarm(Z)

surf = ax.plot_surface(X,Y,B, rstride=1,cstride=1,
               facecolors=fcolors, vmin=minn,vmax=maxx, shade=False)
pcm = ax.pcolor(X,Y,B, data=Z, vmin=minn, vmax=minn, cmap= 'coolwarm')
plt.colorbar(pcm)

`with the pcm and plt.colorbar(pcm) lines hashed I am able to generate a 
coloured surface plot`

plt.title('Total Resistance Of Circuit with Three Components')
ax.set_xlabel('Component One Resistance')
ax.set_ylabel('Component Two Resistance')
ax.set_zlabel('Component Three Resistance')
plt.show()


`without the pcm and pltcolorbar(pcm) lines hashed I get the following 
error`


IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis 
(`None`) and integer or boolean arrays are valid indices


`I expect to see a 3d surface plot with a colorbar to the right of the 
plot where blue is for the lowest values and red for the highest values 
but instead my output is the surface plot with none of the labels when 
the pcm and plt lines are hashed`

1 Ответ

0 голосов
/ 25 апреля 2019

Согласно этому ответу и комментариям под ним, ниже приведен один из способов заставить его работать

surf = ax.plot_surface(X,Y,B, rstride=1,cstride=1,
               facecolors=fcolors, vmin=minn,vmax=maxx, shade=False)
m = cm.ScalarMappable(cmap='coolwarm', norm=surf.norm)
m.set_array(B)
plt.colorbar(m)

enter image description here

...