简体   繁体   English

Matplotlib 条形图 x 轴下方的负值

[英]Matplotlib bar chart negative values below x-axis

I'm new to using Matplotlib.我是使用 Matplotlib 的新手。 I'm trying to build a chart where values can also be negative.我正在尝试构建一个图表,其中的值也可以为负。 Using the generic graph from matplotlib使用来自 matplotlib 的通用图形

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
y_pos = np.arange(len(objects))
performance = [10,8,6,-4,2,1]

plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Usage')
plt.title('Programming language usage')

plt.show()

This produces这产生

在此处输入图片说明

However, I would like to use x-axis as y=0 line instead of having separate y=0.但是,我想使用 x 轴作为 y=0 线而不是单独的 y=0。 So for any negative values, it would appear below x-axis and for positive values, it will appear above x-axis.因此,对于任何负值,它会出现在 x 轴下方,而对于正值,它会出现在 x 轴上方。

It would somehow look like this.它会以某种方式看起来像这样。

在此处输入图片说明

I've managed to get rid of the surrounding lines and values on y-axis.我设法摆脱了 y 轴上的周围线条和值。 Need to know how to make the x-axis the y=0 line.需要知道如何使 x 轴成为 y=0 线。

Any help would be appreciated.任何帮助,将不胜感激。

Thank you so much in advance.非常感谢你。

From here it's reasonably straightforward by accessing the axes object and modifying the spines , you just have to expose the Axes object first with the plt.gca() method.这里开始,通过访问轴对象并修改spines是相当简单的,您只需要首先使用plt.gca()方法公开Axes对象。

The downside here is that getting the xticklabels how you've put them is a bit trickier, but is just a case of placing the relevant text on the Axes and then repeating that for the xlabel .这里的缺点是获取 xticklabels 如何放置它们有点棘手,但这只是将相关文本放在Axes然后对xlabel重复的xlabel You could always try using the labelpad argument for plt.xlabel() but I've not really played much with that.你总是可以尝试使用plt.xlabel()labelpad参数,但我并没有真正玩过它。

import matplotlib.pyplot as plt 
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
y_pos = np.arange(len(objects))
performance = [10,8,6,-4,2,1]

plt.bar(y_pos, performance, align='center', alpha=0.5)
# Get the axes object
ax = plt.gca()
# remove the existing ticklabels
ax.set_xticklabels([])
# remove the extra tick on the negative bar
ax.set_xticks([idx for (idx, x) in enumerate(performance) if x > 0])
ax.spines["bottom"].set_position(("data", 0))
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
# placing each of the x-axis labels individually
label_offset = 0.5
for language, (x_position, y_position) in zip(objects, enumerate(performance)):
    if y_position > 0:
        label_y = -label_offset
    else:
        label_y = y_position - label_offset
    ax.text(x_position, label_y, language, ha="center", va="top")
# Placing the x-axis label, note the transformation into `Axes` co-ordinates
# previously data co-ordinates for the x ticklabels
ax.text(0.5, -0.05, "Usage", ha="center", va="top", transform=ax.transAxes)

plt.show()

Result:结果:

上面代码的条形图输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM