在matplotlib中动态控制轴 [英] control axes in matplotlib dynamically
问题描述
我正在尝试使用matplotlib生成热图.
I'm trying to generate a heat map using matplotlib.
import matplotlib.pyplot as plt
import numpy as np
import sys
import re
data = np.random.rand(10, 10)
heatmap = plt.pcolor(data)
with open(sys.argv[1],'r') as iFile:
for line in iFile:
line=line.strip()
values=re.split('[-:]',line)
x=values[0].strip()
y=values[1].strip()
z=values[2].strip()
data[y,x]=z
for y in range(data.shape[0]):
for x in range(data.shape[1]):
plt.text(x + 0.5, y + 0.5, '%.2f' % data[y, x],
horizontalalignment='center',
verticalalignment='center',)
plt.colorbar(heatmap)
plt.show()
并且数据采用"x-y:值"格式,如下所示
and the data is in the "x - y : value" format as follows
6 - 4 : 0.180671274019055
6 - 1 : 0.30475569499109
6 - 3 : 0.276460025706412
6 - 2 : 0.298002584369681
...
...
1 - 4 : 0.0961071651182192
1 - 1 : 0.259655770346209
1 - 3 : 0.308485173534571
1 - 2 : 0.278724535194018
问题:x和y值非常动态,可以变化(此处为1到6).我想根据输入文件动态设置轴.任何帮助将不胜感激,谢谢!
Question: The x and y values are quite dynamic and can vary (here 1 to 6). I want to dynamically set the axes based on the input file. Any help would be appreciated, thanks!
推荐答案
Pyplot在其API中具有xlim
和ylim
函数.
Pyplot has xlim
and ylim
functions in its API.
http://matplotlib.org/api/pyplot_api.html#matplotlib. pyplot.xlim http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.ylim
如果您在该API页面中进行搜索,还有许多其他与轴相关的操作例程.
Also many other axis related manipulation routines if you search around that API page.
这篇关于在matplotlib中动态控制轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!