mpldriver.py
274 lines
| 7.8 KiB
| text/x-python
|
PythonLexer
|
r201 | import numpy | |
|
r209 | import datetime | |
|
r190 | import matplotlib | |
|
r199 | matplotlib.use("TKAgg") | |
|
r190 | import matplotlib.pyplot | |
|
r209 | import matplotlib.dates | |
|
r197 | #import scitools.numpyutils | |
|
r192 | from mpl_toolkits.axes_grid1 import make_axes_locatable | |
|
r190 | ||
|
r210 | from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter | |
from matplotlib.ticker import FuncFormatter | |||
from matplotlib.ticker import * | |||
|
r209 | ||
|
r201 | ########################################### | |
#Actualizacion de las funciones del driver | |||
########################################### | |||
def createFigure(idfigure, wintitle, width, height, facecolor="w"): | |||
matplotlib.pyplot.ioff() | |||
|
r227 | fig = matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor) | |
|
r201 | fig.canvas.manager.set_window_title(wintitle) | |
fig.canvas.manager.resize(width, height) | |||
matplotlib.pyplot.ion() | |||
return fig | |||
|
r206 | def closeFigure(): | |
matplotlib.pyplot.ioff() | |||
matplotlib.pyplot.show() | |||
|
r207 | return | |
|
r206 | ||
|
r209 | def saveFigure(fig, filename): | |
fig.savefig(filename) | |||
|
r201 | def setWinTitle(fig, title): | |
fig.canvas.manager.set_window_title(title) | |||
def setTitle(fig, title): | |||
fig.suptitle(title) | |||
def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan): | |||
matplotlib.pyplot.figure(fig.number) | |||
axes = matplotlib.pyplot.subplot2grid((nrow, ncol), | |||
(xpos, ypos), | |||
colspan=colspan, | |||
rowspan=rowspan) | |||
return axes | |||
def setAxesText(ax, text): | |||
ax.annotate(text, | |||
xy = (.1, .99), | |||
xycoords = 'figure fraction', | |||
horizontalalignment = 'left', | |||
verticalalignment = 'top', | |||
fontsize = 10) | |||
def printLabels(ax, xlabel, ylabel, title): | |||
ax.set_xlabel(xlabel, size=11) | |||
ax.set_ylabel(ylabel, size=11) | |||
ax.set_title(title, size=12) | |||
|
r204 | def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', | |
ticksize=9, xtick_visible=True, ytick_visible=True, | |||
nxticks=4, nyticks=10, | |||
grid=None): | |||
|
r201 | ||
|
r204 | """ | |
Input: | |||
grid : None, 'both', 'x', 'y' | |||
""" | |||
|
r201 | ax.plot(x, y) | |
ax.set_xlim([xmin,xmax]) | |||
ax.set_ylim([ymin,ymax]) | |||
printLabels(ax, xlabel, ylabel, title) | |||
|
r204 | ###################################################### | |
|
r229 | if (xmax-xmin)<=1: | |
xtickspos = numpy.linspace(xmin,xmax,nxticks) | |||
xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos]) | |||
ax.set_xticks(xtickspos) | |||
else: | |||
xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) | |||
ax.set_xticks(xtickspos) | |||
|
r204 | ||
for tick in ax.get_xticklabels(): | |||
tick.set_visible(xtick_visible) | |||
|
r201 | ||
for tick in ax.xaxis.get_major_ticks(): | |||
tick.label.set_fontsize(ticksize) | |||
|
r204 | ||
###################################################### | |||
for tick in ax.get_yticklabels(): | |||
tick.set_visible(ytick_visible) | |||
for tick in ax.yaxis.get_major_ticks(): | |||
tick.label.set_fontsize(ticksize) | |||
|
r212 | ||
iplot = ax.lines[-1] | |||
|
r204 | ||
###################################################### | |||
|
r212 | if '0.' in matplotlib.__version__[0:2]: | |
print "The matplotlib version has to be updated to 1.1 or newer" | |||
return iplot | |||
if '1.0.' in matplotlib.__version__[0:4]: | |||
print "The matplotlib version has to be updated to 1.1 or newer" | |||
return iplot | |||
|
r204 | if grid != None: | |
ax.grid(b=True, which='major', axis=grid) | |||
|
r201 | matplotlib.pyplot.tight_layout() | |
return iplot | |||
def pline(iplot, x, y, xlabel='', ylabel='', title=''): | |||
ax = iplot.get_axes() | |||
printLabels(ax, xlabel, ylabel, title) | |||
|
r239 | set_linedata(ax, x, y, idline=0) | |
def addpline(ax, x, y, color, linestyle, lw): | |||
ax.plot(x,y,color=color,linestyle=linestyle,lw=lw) | |||
def set_linedata(ax, x, y, idline): | |||
ax.lines[idline].set_data(x,y) | |||
|
r229 | ||
def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, | |||
ticksize=9, xtick_visible=True, ytick_visible=True, | |||
nxticks=4, nyticks=10, | |||
grid=None): | |||
""" | |||
Input: | |||
grid : None, 'both', 'x', 'y' | |||
""" | |||
lines = ax.plot(x.T, y) | |||
leg = ax.legend(lines, legendlabels, loc='upper left') | |||
leg.get_frame().set_alpha(0.5) | |||
ax.set_xlim([xmin,xmax]) | |||
ax.set_ylim([ymin,ymax]) | |||
printLabels(ax, xlabel, ylabel, title) | |||
xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) | |||
ax.set_xticks(xtickspos) | |||
for tick in ax.get_xticklabels(): | |||
tick.set_visible(xtick_visible) | |||
for tick in ax.xaxis.get_major_ticks(): | |||
tick.label.set_fontsize(ticksize) | |||
for tick in ax.get_yticklabels(): | |||
tick.set_visible(ytick_visible) | |||
for tick in ax.yaxis.get_major_ticks(): | |||
tick.label.set_fontsize(ticksize) | |||
iplot = ax.lines[-1] | |||
if '0.' in matplotlib.__version__[0:2]: | |||
print "The matplotlib version has to be updated to 1.1 or newer" | |||
return iplot | |||
if '1.0.' in matplotlib.__version__[0:4]: | |||
print "The matplotlib version has to be updated to 1.1 or newer" | |||
return iplot | |||
if grid != None: | |||
ax.grid(b=True, which='major', axis=grid) | |||
matplotlib.pyplot.tight_layout() | |||
return iplot | |||
def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''): | |||
ax = iplot.get_axes() | |||
printLabels(ax, xlabel, ylabel, title) | |||
|
r201 | ||
|
r229 | for i in range(len(ax.lines)): | |
line = ax.lines[i] | |||
line.set_data(x[i,:],y) | |||
|
r210 | def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, | |
xlabel='', ylabel='', title='', ticksize = 9, | |||
|
r227 | colormap='jet',cblabel='', cbsize="5%", | |
|
r211 | XAxisAsTime=False): | |
|
r201 | ||
divider = make_axes_locatable(ax) | |||
|
r211 | ax_cb = divider.new_horizontal(size=cbsize, pad=0.05) | |
|
r201 | fig = ax.get_figure() | |
fig.add_axes(ax_cb) | |||
ax.set_xlim([xmin,xmax]) | |||
ax.set_ylim([ymin,ymax]) | |||
printLabels(ax, xlabel, ylabel, title) | |||
|
r227 | imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) | |
|
r201 | cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) | |
cb.set_label(cblabel) | |||
|
r210 | # for tl in ax_cb.get_yticklabels(): | |
# tl.set_visible(True) | |||
|
r201 | ||
for tick in ax.yaxis.get_major_ticks(): | |||
tick.label.set_fontsize(ticksize) | |||
for tick in ax.xaxis.get_major_ticks(): | |||
tick.label.set_fontsize(ticksize) | |||
for tick in cb.ax.get_yticklabels(): | |||
tick.set_fontsize(ticksize) | |||
ax_cb.yaxis.tick_right() | |||
|
r212 | ||
if '0.' in matplotlib.__version__[0:2]: | |||
print "The matplotlib version has to be updated to 1.1 or newer" | |||
return imesh | |||
if '1.0.' in matplotlib.__version__[0:4]: | |||
print "The matplotlib version has to be updated to 1.1 or newer" | |||
return imesh | |||
|
r201 | matplotlib.pyplot.tight_layout() | |
|
r210 | if XAxisAsTime: | |
|
r224 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) | |
|
r210 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
ax.xaxis.set_major_locator(LinearLocator(7)) | |||
|
r201 | return imesh | |
|
r190 | ||
|
r201 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): | |
z = z.T | |||
ax = imesh.get_axes() | |||
printLabels(ax, xlabel, ylabel, title) | |||
imesh.set_array(z.ravel()) | |||
|
r190 | ||
|
r229 | def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): | |
|
r210 | ||
printLabels(ax, xlabel, ylabel, title) | |||
|
r229 | imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) | |
|
r207 | ||
|
r201 | def draw(fig): | |
if type(fig) == 'int': | |||
raise ValueError, "This parameter should be of tpye matplotlib figure" | |||
fig.canvas.draw() |