用 Python 创建信息图

发布于 2024-08-09 11:12:39 字数 236 浏览 8 评论 0原文

我想用 python 创建一个简单的信息图。 Matplotlib 似乎有很多功能,但没有什么可以覆盖我的简单热图网格示例。

该信息图是一个简单的 5 x 5 网格,内部数字范围为 0 到 1。网格方块将被着色为 0=白色 1=蓝色 0.5(浅蓝色)。

可能可以使用 Matplotlib,但我找不到或无法结合任何可以深入了解生成此内容的示例。

任何见解、示例代码或库方向都会真正有

帮助 马特

I want to create a simple infographic in python. Matplotlib seems to have a lot of features but nothing that covers off my simple heatmap grid example.

The infographic is a simple 5 x 5 grid with numbers inside ranging from 0 to 1. The grid squares would then be coloured in 0=white 1=blue 0.5 being a pale blue.

Matplotlib could probably be used but I couldn't find or combine any examples that offered insight into generating this.

Any insight, example code or library direction would really help

Regards
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青瓷清茶倾城歌 2024-08-16 11:12:39

这取决于您拥有图表后需要对图表执行什么操作, Matplotlib 允许您以交互方式在屏幕上显示图形,将其保存为矢量、pdf 或位图格式等。

如果您选择此框架,imshow 将满足您的需要,这里有一个示例:

# 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)

有关颜色图的更多详细信息检查此链接,这里是 imshow 链接 - 或者简单地使用 help(colors.LinearSegmentedColormap)help(imshow).

替代文本 http://img522.imageshack.us/img522/6230/bluep.png< /a>

(请注意,这是使用标准选项的结果,您可以添加网格、更改过滤等)。


编辑

但是我想显示
网格中的数字

为了简单起见:

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")

It depends what you need to do with the graph once you have it, Matplotlib allows you to interactively show the graph on the screen, save it in either vector, pdf or bitmap format, and more.

If you opt for this framework, imshow will do what you need, here is an example:

# 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)

For further details on the colormap check this link, and here is the link for imshow - or simply use help(colors.LinearSegmentedColormap) and help(imshow).

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

(note that this is the result with the standard options, you can add a grid, change the filtering and so on).


Edit

however I'm looking to display the
numbers in the grid

To keep it 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")
末が日狂欢 2024-08-16 11:12:39

PyCairo 是你的朋友。简单示例:

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)

output

您可能会发现 本教程很有帮助。

PyCairo is your friend. Simple example:

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)

output

You might find this tutorial helpful.

千纸鹤带着心事 2024-08-16 11:12:39

一种可能性是从 python 生成 SVG。您可以在 Firefox 或 Inkscape 中查看 SVG。

这是一个简单的示例:

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>')

替代文本 http://www.imagechicken.com/uploads /1257184721026098800.png

One possibility would be to generate SVG from python. You can view SVG in Firefox or Inkscape.

Here's a quick-and-dirty example:

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>')

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文