如何使用 matplotlib 创建这种网格表 [英] How do I create this kind of grid table using matplotlib

查看:28
本文介绍了如何使用 matplotlib 创建这种网格表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to using matplotlib. I am trying to create a 2D grid, using matplotlib. This is my first time using matplotlib for anything non trivial.

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

I have decided to break the task into 3 parts:

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

  1. Create the grid table (shown below), color the appropriate columns, and label the axis correctly. This is the one I need most help with. My initial idea is to hold the data for the table in a list of dictionaries (or list of lists); the data struct could hold some meta data about which columns were colored, and then I could simply create the matplot plot off of that data - but I haven't really done any plotting with matplotlib and could do with some help in getting started.

在坐标为(row,col)的网格单元中绘制符号(例如"X")

Plot a symbol (say 'X') in a grid cell with coordinates (row,col)

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

Save the grid table as a picture (this one is easy, I can do by myself)

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

Here is a picture of the kind of grid table I am looking to create using matplotlib:

对于能帮助我入门的任何帮助,我将深表感谢.

I will be very grateful for any help that gets me started.

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

PS: the image is not rendering very well. The horizontal lines in the table are all the same weight (i.e. thickness), so the visual effect of the grid table should look like an Excel worksheet.

[]

请澄清一下,我正在尝试创建的是一种象棋"游戏板.我已经设法修改了里卡多在他的答案中发布的代码片段,以使我尽可能地(以我有限的matplotlib技能!)接近该游戏板.但是,有一些东西缺失":

Just to clarify, what I'm trying to create is a kind of 'chess like' game board. I have managed to modify the code snippet Ricardo posted in his answer, to get as close as I can (with my limited matplotlib skills!) to this game board as I can. However, there are a couple of things "missing":

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

  1. The x axis column labels are strings, not numbers, they are string labels e.g. AB1, AB2, AB3 etc. Additionally, these labels are midpoints (i.e. they are centered, or lie BETWEEN the x axis ticks - not on the ticks themselves)

我需要在给定的 y 轴值的特定列中写入符号,例如,我可能想在 'AB2' 列的 y 轴值 -1565.5 处写入文本 'foo'.

I need to be write symbols in a particular column for a given y axis value, for example, I may want to write the text 'foo' at y axis value -1565.5 in column 'AB2'.

有了这个,我确信我将能够一起破解一些东西,以进入我想要编写的游戏-特别是因为我刚刚为Python开发人员购买了Matplotlib副本.

Once I have this, I am sure I will be able to hack something together to get to the game I am trying to write - especially, since I have just bought a copy of Matplotlib for Python developers.

推荐答案

嗯...我想你可以通过让 matplotlib 显示网格,并将条形图(为列着色)与散点图结合来实现这一点,或者直接文字绘图,用于符号

Mmh... I guess you could achieve this by asking matplotlib to show the grid, and combining a barplot (to color the columns) with a scatter plot or direct text drawing, for the symbol(s)

这可以帮助您入门.不过需要在蜱虫上做一些工作.

This may help you getting started. Needs some work on the ticks, though.

#!/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()

这篇关于如何使用 matplotlib 创建这种网格表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆