我正在尝试使用matplotlib和python构建一个三维散点图。 问题如下所示:3d图上点的偏移使得无法理解这些点的确切位置。
在此图中,请注意这些点与顶点不完全一致。也就是说,根据图表,它显示存在介于0B和1B与4S之间的点,该点不包含在数据集中(参见下面的代码MWE)。是否需要设置偏移量?
#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from mpl_toolkits.mplot3d import Axes3D
import string
from matplotlib import cm
import matplotlib
def plot_state_transition():
xTickMarks = ["-1B","0B", "1B", "2B", "3B"]#, "4B"]#, "1B3S", "2B2S"]
yTickMarks = ["-1S","0S", "1S", "2S", "3S", "4S"]
zTickMarks = ["0", "0.6", "0.65", "0.9", "1.15"]
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 10})
fig = plt.figure(figsize=(11.69,4.88)) # for landscape
axes1 = fig.add_subplot(111, projection='3d')
savename = "state-transition.png"
tup = []
plt.grid(True,linestyle='-',color='0.75')
X_AXIS = ['2B', '2B', '1B', '2B', '2B', '2B', '1B', '2B']
Y_AXIS = ['0S', '2S', '3S', '2S', '2S', '2S', '3S', '2S']
Z_AXIS = ['0.6', '0.6', '0.6', '0.6', '0.6', '0.9', '0.9', '0.9']
s = [12.900648500000001, 12.705360163934426, 13.021028032786887, 13.258014354838707, 14.418979838709676, 17.092295806451613, 15.625246451612906, 17.484319354838711]
x = [xTickMarks.index(i) for i in X_AXIS]
y = [yTickMarks.index(i) for i in Y_AXIS]
z = [zTickMarks.index(i) for i in Z_AXIS]
s = s
axes1.scatter(x, y, z, c='r', marker='o')
axes1.set_xlim((0, len(xTickMarks)-1))
axes1.set_ylim((0, len(yTickMarks)-1))
axes1.set_zlim((0, len(zTickMarks)-1))
axes1.set_xticks(xrange(len(xTickMarks)))
axes1.set_yticks(xrange(len(yTickMarks)))
axes1.set_zticks(xrange(len(zTickMarks)))
axes1.set_xticklabels(xTickMarks)
axes1.set_yticklabels(yTickMarks)
axes1.set_zticklabels(zTickMarks)
axes1.set_ylabel('Small cores')
axes1.set_zlabel('Frequency')
axes1.set_xlabel('Big cores')
axes1.xaxis.grid(True)
figsize=(11.69,8.27) # for landscape
fig.savefig(savepath + savename, bbox_inches='tight', dpi=300, pad_inches=0.1)
plt.clf()
def main():
plot_state_transition()
if __name__ == "__main__":
main()