Python 如何使用matplotlib创建这种网格表

Python 如何使用matplotlib创建这种网格表,python,matplotlib,Python,Matplotlib,我不熟悉使用matplotlib。我正在尝试使用matplotlib创建二维栅格。这是我第一次将matplotlib用于任何非平凡的事情 我决定将任务分为三个部分: 创建网格表(如下所示),为相应的列着色,并正确标记轴。这是我最需要帮助的一个。我最初的想法是将表的数据保存在字典列表(或列表列表)中;data struct可以保存一些关于哪些列被着色的元数据,然后我可以简单地根据这些数据创建matplot绘图-但是我还没有使用matplotlib进行任何绘图,可以在入门时获得一些帮助 用坐标(行、

我不熟悉使用matplotlib。我正在尝试使用matplotlib创建二维栅格。这是我第一次将matplotlib用于任何非平凡的事情

我决定将任务分为三个部分:

  • 创建网格表(如下所示),为相应的列着色,并正确标记轴。这是我最需要帮助的一个。我最初的想法是将表的数据保存在字典列表(或列表列表)中;data struct可以保存一些关于哪些列被着色的元数据,然后我可以简单地根据这些数据创建matplot绘图-但是我还没有使用matplotlib进行任何绘图,可以在入门时获得一些帮助

  • 用坐标(行、列)在网格单元中绘制符号(如“X”)

  • 将表格另存为图片(这张很简单,我可以自己做)

  • 下面是我希望使用matplotlib创建的网格表类型的图片:

    我将非常感谢任何能让我开始的帮助

    PS:图像的渲染效果不是很好。表格中的水平线都具有相同的权重(即厚度),因此网格表格的视觉效果应类似于Excel工作表

    [[编辑/更新]]

    我只是想澄清一下,我想创造的是一种“象棋式”的棋盘。我已经设法修改了里卡多在回答中的代码片段,以尽可能接近这个游戏板(用我有限的matplotlib技能!)。然而,有两件事“遗漏”:

  • x轴列标签是字符串,而不是数字,它们是字符串标签,例如AB1、AB2、AB3等。此外,这些标签是中点(即它们居中,或位于x轴刻度之间,而不是刻度本身)

  • 我需要为给定的y轴值在特定列中写入符号,例如,我可能希望在“AB2”列中的y轴值-1565.5处写入文本“foo”


  • 一旦我有了这个,我相信我将能够一起攻克一些东西,进入我试图编写的游戏——特别是,因为我刚刚为Python开发人员购买了Matplotlib的副本。

    嗯。。。我想您可以通过要求matplotlib显示网格,并为符号组合条形图(为列着色)和散点图或直接文本绘图来实现这一点

    编辑:这可能有助于您入门。不过,需要对蜱虫进行一些研究

    #!/usr/bin/python
    
    from pylab import *
    import matplotlib
    import matplotlib.ticker as ticker
    
    # Setting minor ticker size to 0, globally.
    # Useful for our example, but may not be what
    # you want, always
    matplotlib.rcParams['xtick.minor.size'] = 0
    
    # Create a figure with just one subplot.
    # 111 means "1 row, 1 column, 1st subplot"
    fig = figure()
    ax = fig.add_subplot(111)
    # Set both X and Y limits so that matplotlib
    # don't determine it's own limits using the data
    ax.set_xlim(0, 800)
    
    # Fixes the major ticks to the places we want (one every hundred units)
    # and removes the labels for the majors: we're not using them!
    ax.xaxis.set_major_locator(ticker.FixedLocator(range(0, 801, 100)))
    ax.xaxis.set_major_formatter(ticker.NullFormatter())
    # Add minor tickers AND labels for them
    ax.xaxis.set_minor_locator(ticker.AutoMinorLocator(n=2))
    ax.xaxis.set_minor_formatter(ticker.FixedFormatter(['AB%d' % x for x in range(1, 9)]))
    
    ax.set_ylim(-2000,6500, auto = False)
    # And set the grid!
    ax.grid(True, linestyle='-')
    
    # common attributes for the bar plots
    bcommon = dict(
        height = [8500],  # Height = 6500 - (-2000)
        bottom = -2000,   # Where to put the bottom of the plot (in Y)
        width = 100)      # This is the width of each bar, itself
                          # determined by the distance between X ticks
    
    # Now, we create one separate bar plot pear colored column
    # Each bar is a rectangle specified by its bottom left corner
    # (left and bottom parameters), a width and a height. Also, in
    # your case, the color. Three of those parameters are fixed: height,
    # bottom and width; and we set them in the "bcommon" dictionary.
    # So, we call bar with those two parameters, plus an expansion of
    # the dictionary.
    
    # Note that both "left" and "height" are lists, not single values.
    # That's because each barplot could (potentially) have a number of
    # bars, each one with a left starting point, along with its height.
    # In this case, there's only one pair left-height per barplot.
    bars = [[600, 'blue'],
            [700, 'orange']]
    for left, clr in bars:
        bar([left], color=clr, **bcommon)
    
    show()
    

    +1用于代码段!。酷!。我很惊讶,这么短的一段代码几乎完全实现了我在第一个需求中的目标!。现在试着去理解你写的东西吧。。。呃,请你加上一些评论,让像我这样的凡人都能理解代码:)谢谢!那里我还修改了条形图,这样你就可以在每个彩色栏中添加更少的代码。谢谢你的评论!我已经设法对你的代码做了一些调整,使它更加指向我想要的方向。尽管如此,我仍然需要一些帮助-例如,X轴的“居中”文本标签(希望很容易?),以及在指定的坐标(列名,y值)处写入文本。请看我的最新问题。我们可以做奇迹调整主要/次要刻度和标签。让我快速想出一些办法……现在,看看变化:我已经修复了X轴的标记。通过删除主定位器的标签并添加辅定位器(
    n=2
    意味着将两个主定位器之间的空间划分为两个区域,这意味着辅定位器将居中!)及其标签,我们可以获得所需的效果:)。你可以用这个例子来做Y!