问题
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()
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
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!
回答1:
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
Also many other axis related manipulation routines if you search around that API page.
来源:https://stackoverflow.com/questions/17245011/control-axes-in-matplotlib-dynamically