简体   繁体   English

从列表中读取标签时,我不知道如何设置多行 xticklabels - matplotlib

[英]I can't figure out how to set multi-line xticklabels when reading labels from a list - matplotlib

As the title suggests, I am having trouble finding any documentation on how to print multi-line xticklabels onto my graph when reading from two separate lists.正如标题所暗示的那样,从两个单独的列表中读取时,我无法找到有关如何将多行 xticklabels 打印到我的图表上的任何文档。 I have heard theories on how to make a separate invisible x-axis and then set the second row of labels off of that, but I have yet to see any code that would explain how that would be done.我听说过关于如何制作一个单独的不可见 x 轴然后设置第二行标签的理论,但我还没有看到任何代码可以解释如何做到这一点。 Thoughts?想法? Thanks for your time.谢谢你的时间。

from inventoryClass import stockItem

import numpy as np
import matplotlib.pyplot as plt

def plotInventory(itemRecords) :

    stockBegin = []
    stockFinish = []  
    stockID = []   
    stockItems = []
    for rec in itemRecords.values() :
        stockBegin.append(rec.getStockStart())
        stockFinish.append(rec.getStockOnHand())
        stockID.append(rec.getID())
        stockItems.append(rec.getName())
    N = len(stockBegin)    
    tuple(stockBegin)
    tuple(stockFinish)

    ind = np.arange(N)  # the x locations for the groups
    width = 0.35       # the width of the bars

    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, stockBegin, width, color='r')
    rects2 = ax.bar(ind + width, stockFinish, width, color='c')
    ymax = ax.get_ylim()    
    ax.set_ylim(0, (max(ymax) + 30))    



    # add some text for labels, title and axes ticks
    ax.set_ylabel('Inventory')
    ax.set_title('Stock start and end inventory, by item')
    ax.set_xticks(ind + width)
    ax.set_xticklabels(stockItems)
    ax.set_xticklabels(stockID)    
    ax.legend((rects1[0], rects2[0]), ('Start', 'End'))

    def autolabel(rects) :

        for rect in rects :
            height = rect.get_height()
            ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                    '%d' % int(height),
                ha='center', va='bottom')

    autolabel(rects1)
    autolabel(rects2)

plt.show()

Example of what I am trying to accomplish here:我在这里尝试完成的示例:

在此处输入图片说明

Join your two lists with a new line character.使用换行符连接您的两个列表。 You can pass the list into ax.set .您可以将列表传递给ax.set

import matplotlib.pyplot as plt
xlab1 = [ "a", "b", "c"]
xlab2 = ["1", "2", "3"] 
xlabels = [f"{x1}\n{x2}" for x1, x2, in zip(xlab1,xlab2)]

fig, ax = plt.subplots()
_ = ax.bar([0, 3, 6], range(3), width=1)
_ = ax.bar([1, 4, 7], range(3), width=1)
_ = ax.set(xticks=[.5, 3.5, 6.5], xticklabels=xlabels)

在此处输入图片说明

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

相关问题 EOFError:读取一行时出现EOF,不知道为什么 - EOFError: EOF when reading a line, can't figure out why 如何从 Python 上的 Discord 中的 SQLite 表制作多行列表? - How can I make the multi-line list from SQLite table in Discord on Python? 使用来自另一个 pandas 列 matplotlib 的标签注释多行 plot 中的单行 - annotate a single line from a multi-line plot with labels from another pandas column matplotlib 我似乎无法弄清楚如何更新 tkinter 标签 - I can't seem to figure out how to update tkinter labels 从 Pandas Dataframe 创建 Networkx 有向图时如何创建多线节点标签 - How to create multi-line node labels when creating Networkx Directed Graphs from Pandas Dataframe 如何在matplotlib中设置日期的xticklabels - How to set the xticklabels for date in matplotlib 我怎样才能保存这个 matplotlib 图形,这样 x 轴标签就不会被裁剪掉? - How can I save this matplotlib figure such that the x-axis labels are not cropped out? 无法将多行文本添加为​​一项 - Can't add multi-line text to list as one item 如何在图表中分隔条形并扩展 X-labels 以使其更整洁? 我试过 set_xticklabels 但无济于事 - How to space out the bars in a graph and extend the X-labels to make it neater? I have tried set_xticklabels but to no avail 调用 set_xticklabels 时打印标签 - labels are printed when set_xticklabels called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM