左右翻转 Plotly 水平直方图

Flip left-right Plotly Horizontal Histogram

我制作了一个水平直方图,如下图右侧所示。这可以使用 matplotlib 的 hist() 函数中的 orientation 关键字来完成,如下面的代码所示,它生成了以下图表。

import numpy as np
import matplotlib.pyplot as plt

#generate some data
data = np.random.normal(size=100)

#define the plot
fig, ax = plt.subplots()

#plot the data as a histogram
ax.hist(data, orientation=u'horizontal')

#move ticks to the right
ax.yaxis.tick_right()

plt.show()

是否可以翻转 x 轴,使条形底部位于右侧,条形向左延伸,如下图左手图所示?

查看布局的xanchor(选项有'left'、'right' 'center')

https://plot.ly/python/reference/#bar

通过将水平条形图上的 xanchor 设置为右侧,您应该能够获得该效果。

答案很简单,就是反转要镜像的轴的轴限制顺序。在这种特殊情况下,可以像这样达到预期的结果:

import numpy as np
import matplotlib.pyplot as plt

#generate some data
data = np.random.normal(size=100)

#define the plot
fig, ax = plt.subplots()

#plot the data as a histogram
ax.hist(data, orientation=u'horizontal')

#invert the order of x-axis values
ax.set_xlim(ax.get_xlim()[::-1])

#move ticks to the right
ax.yaxis.tick_right()

plt.show()