2012-01-23 15 views
3

我是使用matplotlib的新手。我正在嘗試使用matplotlib創建一個2D網格。這是我第一次使用matplotlib進行任何非平凡的事情。如何使用matplotlib創建這種網格表

我已決定把任務分成3個部分:

  1. 創建網格表(如下所示),顏色相應的列,並正確標註的軸線。這是我最需要幫助的人。我最初的想法是將表中的數據保存在字典列表(或列表列表)中;數據結構可以保存一些關於哪些列被着色的元數據,然後我可以簡單地從該數據創建matplot圖 - 但是我沒有真正使用matplotlib進行任何繪圖,並且可以在入門時獲得一些幫助。

  2. 情節的符號(說「X」)與座標網格單元(行,列)

  3. 保存網格表爲圖片(這一個是容易的,我可以自己做)

這裏是那種網格表中的照片,我希望創建一個使用matplotlib:

matplotlib grid table

我會是v非常感謝任何幫助我開始。

PS:圖像渲染效果不好。表格中的水平線都具有相同的權重(即厚度),因此網格表的視覺效果應該看起來像Excel工作表。

[編輯/更新]

只是爲了澄清,我想要創造是一種遊戲板「如棋」。我已經設法修改了裏卡多在他的答案中發佈的代碼片段,盡我所能地盡我所能(使用我有限的matplotlib技能!)儘可能地將其添加到遊戲板中。但是,有幾件事「遺漏」:

  1. x軸列標籤是字符串,而不是數字,它們是字符串標籤例如AB1,AB2,AB3等。此外,這些標籤是中點(即它們居中或位於x軸刻度之間 - 不在刻度本身上)

  2. 我需要在特定列中寫入符號例如,給定y軸值,我可能想在y軸值-1565.5的'AB2'列中寫入文本'foo'。

一旦我有這個,我相信我將能砍東西一起去比賽,我試圖寫 - 尤其是因爲我剛剛買了Matplotlib的副本Python開發。

回答

4

Mmh的......我想你可以問matplotlib顯示網格,組合barplot(以色列)用散點圖或直接文本繪製,對於符號(S)

實現這一目標

編輯:這可以幫助你開始。不過,需要在蜱蟲方面做一些工作。

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

+1爲片段!涼!。令我驚訝的是,這樣一個簡短的片段幾乎完全實現了我在我的第一個要求中所要做的事情!現在試着瞭解你寫的東西! ...呃,請你補充一些意見,所以像我這樣的凡人可以理解代碼:)謝謝! –

+0

那裏。我也修改了線框圖,以便每個彩色列添加更少的代碼。 –

+0

感謝您的評論!我已經設法稍微調整一下你的代碼,讓它更多地指向我希望它發生的方向。有幾件事情我仍然需要幫助 - 例如'中心'的文本標籤爲X軸(希望容易?)和寫在由(列名,Y值)指定的座標文本。請看我更新的問題。 –