我对如何在3D / 2D矢量场中添加颜色条感到非常困惑。 我的源代码如下:
import copy
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import fieldmap_utils
data = {}
dot = {
"ampX":0,
"ampY":0,
"ampZ":0,
"phaseX":0,
"phaseY":0,
"phaseZ":0,
}
location = {
"X":[],
"Y":[],
"Z":[]
}
def buildFrame(origin, orientation, length):
body = np.dot(orientation, length).tolist()
temp = origin.copy()
for x in body:
temp.append(x)
return temp
dic = fieldmap_utils.load_single_ampphase("EM averaged.csv")
lst = list(dic.index.get_level_values('freq (hz)').unique())
lst.sort()
print("List of available frequencies: ")
for each_freq in lst:
print(each_freq,end = " ")
print()
freq = input("Enter selected frequency: ")
dic = dic[dic.index.get_level_values('freq (hz)') == int(freq)]
for index, row in dic.iterrows():
# Looks like this:
# data[(x,y,z)] = {"ampX":0,,"ampY":0,"ampZ":0,"phaseX":0,"phaseY":0,"phaseZ":0}
data[(index[4][0], index[4][1], index[4][2])] = copy.deepcopy(dot)
for index, row in dic.iterrows():
if (index[7] == "TARGET-X"):
data[(index[4][0], index[4][1], index[4][2])]["ampX"] = row['ampl']
data[(index[4][0], index[4][1], index[4][2])]["phaseX"] = row['phase_deg']
if (index[7] == "TARGET-Y"):
data[(index[4][0], index[4][1], index[4][2])]["ampY"] = row['ampl']
data[(index[4][0], index[4][1], index[4][2])]["phaseY"] = row['phase_deg']
if (index[7] == "TARGET-Z"):
data[(index[4][0], index[4][1], index[4][2])]["ampZ"] = row['ampl']
data[(index[4][0], index[4][1], index[4][2])]["phaseZ"] = row['phase_deg']
colorMap = cm.get_cmap('Greys')
fig = plt.figure(figsize=(12,12))
ax3D = fig.add_subplot(2, 2, 1, projection = '3d')
axX = fig.add_subplot(2, 2, 2)
axY = fig.add_subplot(2, 2, 3)
axZ = fig.add_subplot(2, 2, 4)
positions = list(data.keys())
xloc = set()
yloc = set()
zloc = set()
for each_position in positions:
xloc.add(each_position[0])
yloc.add(each_position[1])
zloc.add(each_position[2])
xlst = list(xloc)
ylst = list(yloc)
zlst = list(zloc)
xlst.sort()
ylst.sort()
zlst.sort()
print("Unique X coordinates:")
for each_x in xlst:
print(each_x,end = " ")
print()
sliceX = int(input("Enter X slice position: "))
print("Unique Y coordinates:")
for each_y in xlst:
print(each_y,end = " ")
print()
sliceY = int(input("Enter Y slice position: "))
print("Unique Z coordinates:")
for each_z in zlst:
print(each_z,end = " ")
print()
sliceZ = int(input("Enter Z slice position: "))
scale = 500
for position in positions:
x, y, z = position
u, v, w = data[position]["ampX"]*scale, data[position]["ampY"]*scale, data[position]["ampZ"]*scale
# orientation = [[data[position]["ampX"], 0, 0],
# [0, data[position]["ampY"], 0],
# [0, 0, data[position]["ampZ"]]]
# x, y, z, u, v, w = buildFrame([position[0], position[1], position[2]], orientation, 5000)
ax3D.quiver(x, y, z, u, v, w)
if x == sliceX:
axX.quiver(y, z, v, w)
if y == sliceY:
axY.quiver(x, z, u, w)
if z == sliceZ:
axZ.quiver(x, y, u, v)
#, cmap = colorMap
ax3D.view_init(azim=50, elev=25)
ax3D.set_xlabel('X')
ax3D.set_ylabel('Y')
ax3D.set_zlabel('Z')
ax3D.set_xlim([-275, 300])
ax3D.set_ylim([-275, 450])
ax3D.set_zlim([0, 500])
axX.set_title('Looking from X-axis')
axX.set_xlabel('Y')
axX.set_ylabel('Z')
axX.set_xlim([-275, 425])
axX.set_ylim([0, 500])
axY.set_title('Looking from Y-axis')
axY.set_xlabel('X')
axY.set_ylabel('Z')
axY.set_xlim([-275, 300])
axY.set_ylim([0, 500])
axZ.set_title('Looking from Z-axis')
axZ.set_xlabel('X')
axZ.set_ylabel('Y')
axZ.set_xlim([-275, 300])
axZ.set_ylim([-275, 450])
# plt.savefig('demo', dpi = 1200)
plt.show()
代码不是经过优化或完美的,仅用作演示。
但是,我对如何在3个2D颤抖图中添加1个彩条感到非常困惑。
我已经阅读了matlibplot的一些文档,但是对于如何添加颜色条我仍然没有一个体面的想法。我尝试使用与散点图中相同的方法,但也没有成功。
谢谢你们的帮助!