创建信息图表在Python
-
12-09-2019 - |
题
我想创建在python一个简单的信息图。 Matplotlib似乎有很多功能,但没有覆盖掉我的简单的热图格的例子。
在信息图是一个简单的5×5网格内的数字范围从0到1。方格将随后在0 =白1 =蓝色0.5是淡蓝色的着色。
Matplotlib可能能够使用,但我无法找到或组合是提供洞察产生任何的例子。
任何了解,示例代码或库方向将真正帮助
此致 马特
解决方案
这取决于你需要做的图形,一旦你拥有了它, 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)
有关颜色表检查此链接,这里是href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow" rel="nofollow noreferrer">链接imshow 的help(colors.LinearSegmentedColormap)和help(imshow)
替代文字http://img522.imageshack.us/img522/6230/bluep .PNG
(注意,这是与标准的选项的结果,则可以添加一格,改变滤波等)。
修改强>
但是我期待以显示 在网格编号
要保持它的简单:
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")
其他提示
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)
您可能会发现本教程有帮助的。
一种可能性是生成从蟒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一>