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

3
我刚开始使用matplotlib,尝试使用matplotlib创建2D网格。这是我第一次在matplotlib中进行非平凡的任何操作。
我已经决定将任务分为三个部分:
1. 创建网格表(如下所示),正确着色并标记轴。这是我最需要帮助的地方。 我的初始想法是将表格数据保存在字典列表(或列表列表)中;数据结构可以保存有关哪些列被着色的元数据,然后我就可以根据该数据简单地创建matplot图 - 但我实际上没有使用matplotlib绘图,并需要一些帮助入门。 2. 在坐标(行、列)处的网格单元格中绘制符号(例如'X')。 3. 将网格表保存为图片(这很容易,我可以自己完成)。
这是我要使用matplotlib创建的网格表的图片:
PS:图像显示不太好。表格中的水平线都具有相同的宽度(即粗细),因此网格表的视觉效果应类似于Excel工作表。
[[编辑/更新]]
只是为了澄清,我尝试创建一种类似“棋盘”的游戏板。我已经设法修改了Ricardo在他的答案中发布的代码片段,以尽可能接近这个游戏板(我的matplotlib技能有限!)。 但是,还有几个“缺失”的地方:
1. x轴列标签是字符串而不是数字,它们是字符串标签(例如AB1、AB2、AB3等)。 此外,这些标签是中点(即它们居中或位于x轴刻度之间 - 不在刻度本身上)。 2. 我需要在给定的y轴值的特定列中编写符号,例如,我可能希望在列'AB2'中的y轴值-1565.5处写入文本'foo'。
一旦我有了这个,我相信我将能够拼凑出我正在尝试编写的游戏 - 特别是当我刚刚买了Matplotlib for Python developers的副本。
1个回答

4

嗯...我猜你可以通过要求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()

谢谢您的评论!我已经稍微调整了您的代码,让它更朝着我想要的方向发展。不过还有一些事情我需要帮助 - 例如 X 轴上的“居中”文本标签(应该很容易吧?)和在指定坐标(列名、y 值)写入文本。请参见我的更新问题。 - Homunculus Reticulli
我们可以通过调整主/次刻度和标签来实现惊人的效果。让我快速想出一些东西... - Ricardo Cárdenes
感谢更新。我仍在努力弄清楚你正在做什么。我尝试了你发布的片段,但出现了以下异常:ax.xaxis.set_minor_locator(ticker.AutoMinorLocator(n=2)) TypeError: this constructor takes no arguments。你知道为什么吗?(顺便说一句,我正在运行 matplotlib 0.99.1.1 和 Python 2.6.5)。 - Homunculus Reticulli
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/7015/discussion-between-ricardo-cardenes-and-homunculus-reticulli - Ricardo Cárdenes
谢谢你的帮助,我想我能够从这里继续 :) - Homunculus Reticulli
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接