【问题标题】:How do I create this kind of grid table using matplotlib如何使用 matplotlib 创建这种网格表
【发布时间】:2012-01-23 15:14:28
【问题描述】:

我是使用 matplotlib 的新手。我正在尝试使用 matplotlib 创建一个 2D 网格。这是我第一次使用 matplotlib 做任何不平凡的事情。

我决定把任务分成三部分:

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

  2. 在坐标为 (row,col) 的网格单元中绘制一个符号(比如“X”)

  3. 将网格表另存为图片(这个很简单,我自己会做)

这是我希望使用 matplotlib 创建的那种网格表的图片:

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

PS:图像渲染得不是很好。表格中的横线都是相同的权重(即粗细),所以网格表格的视觉效果应该类似于 Excel 工作表。

[[编辑/更新]]

澄清一下,我正在尝试创建一种“象棋”的游戏板。我已经设法修改了在他的答案中发布的代码 sn-p Ricardo,以尽可能接近这个游戏板(用我有限的 matplotlib 技能!)。但是,有几件事“缺失”:

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

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

一旦我有了这个,我确信我可以一起破解一些东西来开发我正在尝试编写的游戏 - 特别是因为我刚刚为 Python 开发人员购买了一份 Matplotlib。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    嗯...我想您可以通过让 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 表示 sn-p!。凉爽的!。我很惊讶这么短的 sn-p 几乎完全实现了我在第一个要求中设定的目标!现在试着理解你写的东西! ...呃,你能不能加一些cmets,所以像我这样的凡人都能看懂代码:) 谢谢!
    • 那里。我还修改了条形图,以便为每个彩色列添加更少的代码
    • 感谢 cmets!我已经设法稍微调整了您的代码,以将其更多地指向我想要的方向。不过,有几件事我仍然需要帮助 - 例如。 X 轴的“居中”文本标签(希望很简单?)并在由(列名,y 值)指定的坐标处写入文本。请查看我更新的问题。
    • 我们可以调整主要/次要刻度和标签。让我想出一些快速的东西......
    • 现在,看看变化:我已经修复了 X 轴的代码。通过删除主要的标签并添加次要定位符(n=2 意味着将两个主要之间的空间划分为两个区域,这意味着次要将居中!),以及它们的标签,我们得到了预期的效果:)。你可以用这个例子来做 Y!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2013-06-18
    相关资源
    最近更新 更多