如何在matplotlib中更改x轴和y轴?

6

我正在使用matplotlib绘制神经网络。我找到了一段绘制神经网络的代码,但它是从上到下定向的。我想将其方向改变为从左到右。因此,基本上在已经绘制所有形状之后,我想要更改x和y轴。有没有简单的方法来实现这个目标?

我还发现一个答案说可以将参数"orientation"更改为水平(下面的代码),但我不太明白在我的代码中应该复制到哪里。这样做会给我带来相同的结果吗?

matplotlib.pyplot.hist(x, 
                   bins=10, 
                   range=None, 
                   normed=False, 
                   weights=None, 
                   cumulative=False, 
                   bottom=None, 
                   histtype=u'bar', 
                   align=u'mid', 
                   orientation=u'vertical', 
                   rwidth=None, 
                   log=False, 
                   color=None, 
                   label=None, 
                   stacked=False, 
                   hold=None, 
                   **kwargs)
1个回答

8
您的代码中展示了如何在matplotlib中启动一个直方图的示例。请注意,您使用的是pyplot默认接口(而不一定是构建自己的图形)。
因此,这行代码:
orientation=u'vertical',

should be:

orientation=u'horizontal',

如果您想让条形图从左到右显示,可以使用以下命令。但是这并不能帮助您反转y轴,要实现该功能,请使用以下命令:

plt.gca().invert_yaxis()

下面的示例展示了如何从随机数据构建一个直方图(非对称以易于感知修改)。第一个图是普通直方图,第二个我更改了直方图的方向; 在最后一个图中,我反转了y轴。
import numpy as np
import matplotlib.pyplot as plt

data = np.random.exponential(1, 100)

# Showing the first plot.
plt.hist(data, bins=10)
plt.show()

# Cleaning the plot (useful if you want to draw new shapes without closing the figure
# but quite useless for this particular example. I put it here as an example).
plt.gcf().clear()

# Showing the plot with horizontal orientation
plt.hist(data, bins=10, orientation='horizontal')
plt.show()

# Cleaning the plot.
plt.gcf().clear()

# Showing the third plot with orizontal orientation and inverted y axis.
plt.hist(data, bins=10, orientation='horizontal')
plt.gca().invert_yaxis()
plt.show()

第一个图(plot 1)的结果是(默认直方图):

matplotlib中的默认直方图

第二个图(条形方向已更改):

matplotlib中的默认直方图,已更改方向

最后一个图(反转y轴):

matplotlib中的水平条形直方图,已反转y轴


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接