Matplotlib将x刻度放置在上方

XyledMonkey

有什么方法可以将x点放置在堆叠条形图中的每个堆叠条形上方,而不是在x轴下方?需要明确的是,我并不是说将x-tick放在单个堆叠栏中的上方,我的意思是将x-tick放在堆叠起来的bar本身上方。这是我创建情节的方法:

df = pd.DataFrame(np.random.randint(1, 5, size=(3200, 3)))    
df.loc[np.random.choice(df.index, size=3190, replace=False), :] = 0

df_select = df[df.sum(axis=1)>1]
fig, ax = plt.subplots()

ax.bar(df_select.index, df_select.iloc[:,0], label = df_select.columns[0], wdith = 15)

if df_select.shape[1] > 1:
    for i in range(1, df_select.shape[1]):
        bottom = df_select.iloc[:,np.arange(0,i,1)].sum(axis=1)
        ax.bar(df_select.index, df_select.iloc[:,i], bottom=bottom, label = 
df_select.columns[i], width = 15)

ax.set_xticks(df_select.index)
plt.legend(loc='best', bbox_to_anchor=(1, 0.5))
plt.xticks(rotation=90, fontsize=8) #this puts the x ticks below the x axis

另外,我想在x轴的特定位置放置一些文本。我将这些站点存储在列表中:

sites = [19, 173, 1002] # the number and elements of this list vary

因此,例如,在x = 173处,我想在位置173处放置文本“ site(173)”以及对勾。

供大家参考,我已经张贴图片我当前的代码会产生什么,我想生产什么:电流:https://i.stack.imgur.com/QDlEP.png目标:HTTPS://i.stack .imgur.com / IJJo4.png

戴维

这可以通过使用@Evan链接到这个意见,即例如可以实现这一个重要的一点是autolabel链接示例中命名的函数改变值从Y轴的值显示于x轴的值是容易的,替换heightrect.get_x()棘手的一点是将值放在栏的顶部。条形的总高度可以通过将DataFrame中的值相加得出。

heights = df_select.iloc[:,:].sum(axis=1)

然后需要将其传递给自动标签功能,并用作条的高度。可以使用以下方式删除x刻度标签

ax.get_xaxis().set_ticklabels([])

而且,您可以通过将xax.text的内容sites用作x位置,并将y位置设置为在轴下方(-0.5或类似值),从而在特定站点的x轴下方添加更多文本

综上所述,我们得到以下工作示例:

def autolabel(rects, heights):
    """
    Attach a text label above each bar displaying its height
    Modified for use with a stacked bar chart
    """
    for i, rect in enumerate(rects):
        x = rect.get_x()
        wid = rect.get_width()
        height = heights.values[i]

        ax.text(x + wid/2., 1.05*height,
                '%d' % (int(x) + int((wid/2)+0.5)),
                ha='center', va='bottom', rotation=90)


df = pd.DataFrame(np.random.randint(1, 5, size=(3200, 3)))    
df.loc[np.random.choice(df.index, size=3190, replace=False), :] = 0

df_select = df[df.sum(axis=1)>1]
fig, ax = plt.subplots()

ax.bar(df_select.index, df_select.iloc[:,0], label = df_select.columns[0], width = 15)

if df_select.shape[1] > 1:
    for i in range(1, df_select.shape[1]):
        bottom = df_select.iloc[:,np.arange(0,i,1)].sum(axis=1)
        rects1 = ax.bar(df_select.index, df_select.iloc[:,i], bottom=bottom, label = 
df_select.columns[i], width = 15)

ax.set_xticks(df_select.index)
ax.get_xaxis().set_ticklabels([])   # turn off the x tick labels

plt.legend(loc='best', bbox_to_anchor=(1, 0.5))

heights = df_select.iloc[:, :].sum(axis=1)
autolabel(rects1, heights)

# Select 3 random value to add labels below the x axis. Make sure they are
# within the axis limits
sites = np.random.choice(df_select.index, 3)

for site in sites:
    ax.text(site, -0.5,
        'site(%s)' % site,
        ha='center', va='bottom',fontsize=6)

plt.show()

如下图所示:

在此处输入图片说明

注意:由于条形图非常细且散开,并且可以彼此靠近放置,因此有时看起来会很乱,这意味着值可能开始重叠。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Matplotlib:将网格线保留在图形后面,但将y和x轴保留在上方

将图像放在上方

如何使用VBA将数据透视表过滤器彼此放置在上方?

悬停在上方时如何将放大的div放置在中心?

将 matplotlib 的 pcolomesh 的指定刻度标签居中放置在框中

matplotlib 将刻度与刻度末端对齐

Matplotlib ax.fill_between在上方而不是下方填充

错误的 x 刻度标签 matplotlib

纯CSS:将image2悬停在上方/上方/上方显示image1

Matplotlib:如何在图上方设置刻度标签

将图片相对放置在文字上方或下方。

如何将drawable放在上方/下方布局

如何将水平matplotlib颜色条对象的标签准确地放置在颜色条下方或上方的中心?

如何将特定的 x 轴(次要)刻度标签移动到 matplotlib 中图形的顶部?

如何使用matplotlib将单个标记的刻度添加到x轴?

Matplotlib:将x轴刻度标签向左移动一个位置

是否可以将x轴刻度线与matplotlib直方图中的相应条对齐?

Matplotlib子图标题与x刻度重叠

Matplotlib x轴日期刻度频率

使用matplotlib限制x刻度的频率

刻度之间的Matplotlib绘图市场(X轴)

MatLibPlot:将y轴的刻度标签放置在更高的位置

将轴文本标签放置在刻度之间

如果将鼠标悬停在上方图像上,则下方图像将隐藏

将位置按钮放置在AppBarOverlay的右上方

如何使用css将表单放置在标题图像上方?

将锚文本放置在其绝对定位的伪元素上方

ggplot:将小平面条放置在轴标题上方

如何将文本框放置在桌子上方