Question

Je veux créer un simple infographique en python. Matplotlib semble avoir beaucoup de fonctionnalités, mais rien qui couvre de mon exemple simple de la grille heatmap.

Le infographique est un simple grille 5 x 5 avec des nombres à l'intérieur allant de 0 à 1. Les cases de la grille serait alors colorés en blanc 0 = 1 = bleu 0,5 étant un bleu pâle.

Matplotlib pourrait probablement être utilisé, mais je ne pouvais pas trouver ou de combiner des exemples qui ont offert un aperçu de la génération de ce.

Toute idée, exemple de code ou la direction bibliothèque serait vraiment aider

Cordialement Matt

Était-ce utile?

La solution

Cela dépend de ce que vous devez faire avec le graphique une fois que vous l'avez, Matplotlib permet vous de montrer de manière interactive le graphique sur l'écran, enregistrez-le dans les deux vecteur, format pdf ou bitmap, et plus encore.

Si vous optez pour ce cadre, imshow fera ce dont vous avez besoin, voici un exemple:

# Just some data to test:
from random import gauss
a = [[gauss(0, 10) for i in xrange(0, 5)] for j in xrange(0,5)]

from pylab import * # or just launch "IPython -pylab" from the command line

# We create a custom colormap:
myblue = cm.colors.LinearSegmentedColormap("myblue", {
    'red':   [(0, 1, 1), (1, 0, 0)], 
    'green': [(0, 1, 1), (1, 0, 0)],
    'blue':  [(0, 1, 1), (1, 1, 1)]})

# Plotting the graph:
imshow(a, cmap=myblue)

Pour plus de détails sur le vérifier ce lien , et voici la pour imshow -. ou simplement utiliser help(colors.LinearSegmentedColormap) et help(imshow)

texte alt http://img522.imageshack.us/img522/6230/bluep .png

(notez que ceci est le résultat des options standard, vous pouvez ajouter une grille, changer le filtrage, etc.).


Modifier

  

mais je suis à la recherche pour afficher la   numéros dans la grille

Pour faire simple:

for i in xrange(0,5):
    for j in xrange(0,5):
        text(i, j,
             "{0:5.2f}".format(a[i][j]),
             horizontalalignment="center",
             verticalalignment="center")

Autres conseils

pycairo est votre ami. Exemple simple:

from __future__ import with_statement
import cairo
img = cairo.ImageSurface(cairo.FORMAT_ARGB32,100,100)
g = cairo.Context(img)
for x in range(0,100,10):
    for y in range(0,100,10):
        g.set_source_rgb(.1 + x/100.0, 0, .1 + y/100.0)
        g.rectangle(x,y,10,10)
        g.fill()
with open('test.png','wb') as f:
    img.write_to_png(f)

sortie

Vous pouvez trouver ce tutoriel utile.

Une possibilité serait de générer SVG de python. Vous pouvez voir SVG dans Firefox ou Inkscape.

Voici un exemple rapide et sale:

import random

def square(x, y, value):
    r, g, b = value * 255, value * 255, 255
    s = '<rect x="%d" y="%d" width="1" height="1" style="fill:rgb(%d,%d,%d);"/>' % (x, y, r, g, b)
    t = '<text x="%d" y="%d" font-size=".2" fill="yellow">%f</text>' % (x, y + 1, value)
    return s + '\n' + t

print('''
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="100%" height="100%" version="1.1" viewBox="0 0 5 5"
xmlns="http://www.w3.org/2000/svg">
''')
for x in range(0, 5):
    for y in range(0, 5):
        print(square(x, y, random.random()))

print('</svg>')

texte alt http://www.imagechicken.com/uploads/1257184721026098800.png

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top