【问题标题】:How to flip y axis in a bar3d() plot?如何在 bar3d() 图中翻转 y 轴?
【发布时间】:2014-06-23 08:34:15
【问题描述】:

我使用 bar3d() 绘制 3D 条形图,我想翻转 y 轴。我尝试使用invert_yaxis(),但似乎没有效果。我还尝试使用[::-1] 手动反转列表中的值,但它也没有帮助。它一直以相同的方式显示 3D 条形图。

知道如何翻转y 轴吗?

这是一个示例对我有用(即使是 3D 线图也不行):

from matplotlib.pyplot import *
from mpl_toolkits.mplot3d.axes3d import Axes3D

fig1 = figure(1)

ax11 = subplot(2, 2, 1, projection='3d')
ax11.plot([1, 2, 3, 4], [1, 2, 3, 4])

ax12 = subplot(2, 2, 2, projection='3d')
ax12.invert_xaxis()
ax12.plot([1, 2, 3, 4], [1, 2, 3, 4])

ax21 = subplot(2, 2, 3)
ax21.plot([1, 2, 3, 4])

ax22 = subplot(2, 2, 4)
ax22.invert_xaxis()
ax22.plot([1, 2, 3, 4])

show()

情节是这样的:http://we.tl/cqSsecVy6P

谢谢, 丹尼尔

【问题讨论】:

  • 你可以设置ylim。如果先设置最大值后置零,则 y 轴反转:pylab.ylim(max_y, 0)
  • 谢谢,但不幸的是在我的情况下没有帮助......我认为bar3d() 方法以某种方式忽略了这一系列属性,但不确定......但我已经尝试了所有建议我用谷歌找到了(几十个),但他们都没有对情节产生任何影响。它一直在以y 轴和x 轴从左到右增加的方式进行绘图(使用默认视角),我无法改变它。

标签: matplotlib 3d plot axis flip


【解决方案1】:

如果我正确理解了这个问题,我认为问题在于matplotlib 旋转了 3D 图。要解决这个问题,只需使用ax.view_init(elev, azim) 设置初始视角。拿matplotlib hist3d demo 然后我们就有了

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4)

elements = (len(xedges) - 1) * (len(yedges) - 1)
xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)

xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(elements)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()

ypos_inv = ypos
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
ax.view_init(ax.elev, ax.azim+90)
plt.show()

在这里,我将轴旋转了 90 度,它翻转了一个轴而不是另一个。

【讨论】:

  • 这就像魅力一样!非常感谢,先生! :)
猜你喜欢
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多