matplotlib:在3d条形图上反转y轴

希吉特

我使用以下代码使用matplotlib创建了3d条形图:

fig = plt.figure()
cmap = get_cmap(len(os.listdir(conv1d_kernel_path)))
ax = fig.add_subplot(111, projection='3d')

for f in os.listdir(conv1d_kernel_path):
    step = int(re.findall(r'\d+', f)[0])
    conv1d_kernel_histo[f]['bins'] = convert_bins(30, min_weight, max_weight, conv1d_kernel_histo[f])

    bin_counts = conv1d_kernel_histo[f]['bins'][:, 2]
    width = (max_weight-min_weight)/30 #ToDo change 30 to numbins
    xs = conv1d_kernel_histo[f]['bins'][:, 0] + width / 2

    ax.bar(xs, list(bin_counts), width=width, zs=step, zdir='y', color=cmap(step), ec=cmap(step+20), alpha=0.8)

ax.set_xlabel('weights')
ax.set_ylabel('step')
ax.set_zlabel('count')

plt.show()

目录和转换箱功能并不是那么重要,只是它为我提供了可用来定义输入数据的信息,这些信息可以迭代地传递给ax.bar函数。无论如何,我收到以下输出:

在此处输入图片说明

我想反转标题为“ steps”的轴,这个问题似乎几乎是我所需要的。但是,当我使用ax.invert_yaxis()标题为“ weights”的轴时,它是倒置的。当我交替使用ax.invert_xaxis()同一根轴时。出于好奇,我尝试了一下,ax.invert_zaxis()但效果确实令人满意,然后将整个图表颠倒了。有人能解决这个问题吗?轴反转的另一种方法?谢谢,感谢所有帮助

Divyansh Chaudhary

像这样很好地反转轴限制就ax.set_ylim(150,0)可以了。样地:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


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

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
# Invert Y-Axis
ax.set_ylim(4,-4)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

原版的:

原图

反转Y轴:

倒Y轴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章