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

2024-10-04 15:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉matplotlib。我正在尝试使用matplotlib创建二维网格。这是我第一次使用matplotlib处理任何不重要的事情。在

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

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

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

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

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

matplotlib grid table

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

PS:图像渲染得不是很好。表格中的水平线都是相同的粗细(即粗细),因此网格表格的视觉效果应该看起来像Excel工作表。在

[[编辑/更新]]

我想澄清一下,我要创造的是一种“象棋般”的棋盘。我已经成功地修改了Ricardo在他的答案中发布的代码片段,以尽可能接近(用我有限的matplotlib技能!)尽我所能把它放到游戏板上。然而,有几件事“缺失”:

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

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

一旦我有了这个,我确信我将能够一起破解一些东西,从而得到我正在尝试编写的游戏——尤其是,因为我刚刚为Python开发人员购买了一个Matplotlib的副本。在


Tags: 数据字符串标记网格游戏列表matplotlib符号
1条回答
网友
1楼 · 发布于 2024-10-04 15:24:24

嗯。。。我想您可以通过要求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()

相关问题 更多 >

    热门问题