子图被迫拥有相同的轴

时间:2018-03-08 06:01:17

标签: python plot 3d visualization

最近我正在图中绘制两张图。数据不同,没有共同的内容。但最终的可视化被迫拥有相同的轴,我不明白。

Image here

#################################################################################################
fig, (ax1,ax2) = plt.subplots(1,2, sharey = False, sharex = False)

c = list(len(mydf)*'b')
for i in range(len(c)):
    if mydf['percent'][i] > 0.05:
        c[i] = 'r'
# ax1 = fig.add_subplot(121)
ax1.bar(range(len(mydf['cdf'])), mydf['cdf'], color = c)
ax1.set_xticks(range(len(mydf['cdf'])))
ax1.set_xticklabels(list(mydf['3D_Attri']), rotation=45)

###########################################################################################3

ax2 = fig.add_subplot(122, projection='3d')
xs = mydf['sphere']
ys = mydf['cylinder']
zs = mydf['addition']
ax2.scatter(xs, ys, zs, zdir='z', s=20, c=c, depthshade=True)
ax2.set_xlabel('sphere')
ax2.set_ylabel('cylinder')
ax2.set_zlabel('addition')
plt.show()

1 个答案:

答案 0 :(得分:0)

问题是您在第一行代码中创建了两个子图。在此之后直接放置plt.show()以查看是否已经绘制了错误的轴。这将在以后干扰您的3D图形,您只需将其置于其上。你必须以不同的方式解决这个问题:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
##################################################################################
fig = plt.figure()

c = list(len(mydf)*'b')
for i in range(len(c)):
    if mydf['percent'][i] > 0.05:
        c[i] = 'r'
ax1 = fig.add_subplot(121)
ax1.bar(range(len(mydf['cdf'])), mydf['cdf'], color = c)
ax1.set_xticks(range(len(mydf['cdf'])))
ax1.set_xticklabels(list(mydf['3D_Attri']), rotation=45)
##################################################################################
ax2 = fig.add_subplot(122, projection='3d')
xs = mydf['sphere']
ys = mydf['cylinder']
zs = mydf['addition']
ax2.scatter(xs, ys, zs, zdir='z', s=20, c=c, depthshade=True)
ax2.set_xlabel('sphere')
ax2.set_ylabel('cylinder')
ax2.set_zlabel('addition')
plt.show()

玩具数据集的输出: enter image description here