我是使用matplotlib的新手。我正在尝试使用matplotlib创建一个2D网格。这是我第一次将matplotlib用于任何非常重要的事情。
我决定将任务分为三部分:
创建网格表(如下所示),为相应的列着色,并正确标记轴。这是我最需要帮助的。我最初的想法是将表中的数据保存在字典列表(或列表列表)中;数据结构可以包含一些关于哪些列着色的元数据,然后我可以简单地创建该数据的matplot图 - 但我还没有真正用matplotlib进行任何绘图,并且可以在开始时提供一些帮助。 / p>
在带坐标(行,列)的网格单元格中绘制符号(例如'X')
将网格表保存为图片(这个很容易,我可以自己做)
这是我想要使用matplotlib创建的网格表类型的图片:
我将非常感谢能帮助我开始的任何帮助。
PS:图像渲染效果不佳。表格中的水平线都是相同的重量(即厚度),因此网格表的视觉效果看起来应该像Excel工作表。
[[编辑/更新]]
只是为了澄清一下,我想要创造的是一种'象棋'游戏板。我已经设法修改了里卡多在他的回答中发布的代码片段,尽可能接近我(我有限的matplotlib技能!)尽可能接近这个游戏板。但是,有一些事情“缺失”:
x轴列标签是字符串,而不是数字,它们是字符串标签,例如AB1,AB2,AB3等。此外,这些标签是中点(即它们居中,或位于x轴刻度之间 - 而不是刻度线本身)
我需要在特定列中为给定的y轴值写入符号,例如,我可能想要在'AB2'列中的y轴值-1565.5处写入文本'foo'。
一旦我拥有了这个,我相信我将能够共同攻击我正在尝试编写的游戏 - 特别是,因为我刚刚为Python开发人员购买了Matplotlib的副本。
答案 0 :(得分: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()