mpldriver.py
499 lines
| 13.4 KiB
| text/x-python
|
PythonLexer
|
r1115 | import os | |
|
r245 | import sys | |
|
r1115 | import datetime | |
import numpy | |||
|
r190 | import matplotlib | |
|
r264 | ||
|
r1115 | if 'BACKEND' in os.environ: | |
matplotlib.use(os.environ['BACKEND']) | |||
elif 'linux' in sys.platform: | |||
|
r1075 | matplotlib.use("TkAgg") | |
|
r1115 | elif 'darwin' in sys.platform: | |
matplotlib.use('TkAgg') | |||
else: | |||
from schainpy.utils import log | |||
log.warning('Using default Backend="Agg"', 'INFO') | |||
matplotlib.use('Agg') | |||
|
r1075 | # Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX' | |
|
r190 | import matplotlib.pyplot | |
|
r264 | from mpl_toolkits.axes_grid1 import make_axes_locatable | |
|
r696 | from matplotlib.ticker import FuncFormatter, LinearLocator | |
|
r209 | ||
|
r201 | ########################################### | |
|
r1075 | # Actualizacion de las funciones del driver | |
|
r201 | ########################################### | |
|
r873 | # create jro colormap | |
|
r858 | jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90] | |
|
r1075 | blu_values = matplotlib.pyplot.get_cmap( | |
"seismic_r", 20)(numpy.arange(20))[10:15] | |||
ncmap = matplotlib.colors.LinearSegmentedColormap.from_list( | |||
"jro", numpy.vstack((blu_values, jet_values))) | |||
|
r858 | matplotlib.pyplot.register_cmap(cmap=ncmap) | |
|
r1075 | ||
def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi=80): | |||
|
r858 | ||
|
r201 | matplotlib.pyplot.ioff() | |
|
r858 | ||
|
r1075 | fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=( | |
1.0 * width / dpi, 1.0 * height / dpi)) | |||
|
r201 | fig.canvas.manager.set_window_title(wintitle) | |
|
r828 | # fig.canvas.manager.resize(width, height) | |
|
r201 | matplotlib.pyplot.ion() | |
|
r858 | ||
|
r342 | if show: | |
matplotlib.pyplot.show() | |||
|
r858 | ||
|
r201 | return fig | |
|
r1075 | ||
|
r587 | def closeFigure(show=False, fig=None): | |
|
r858 | ||
|
r1075 | # matplotlib.pyplot.ioff() | |
# matplotlib.pyplot.pause(0) | |||
|
r858 | ||
|
r342 | if show: | |
matplotlib.pyplot.show() | |||
|
r858 | ||
|
r587 | if fig != None: | |
|
r706 | matplotlib.pyplot.close(fig) | |
# matplotlib.pyplot.pause(0) | |||
|
r672 | # matplotlib.pyplot.ion() | |
|
r706 | ||
|
r587 | return | |
|
r858 | ||
|
r587 | matplotlib.pyplot.close("all") | |
|
r706 | # matplotlib.pyplot.pause(0) | |
|
r672 | # matplotlib.pyplot.ion() | |
|
r706 | ||
|
r207 | return | |
|
r206 | ||
|
r1075 | ||
|
r209 | def saveFigure(fig, filename): | |
|
r858 | ||
|
r1075 | # matplotlib.pyplot.ioff() | |
|
r828 | fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi) | |
|
r672 | # matplotlib.pyplot.ion() | |
|
r209 | ||
|
r1075 | ||
|
r760 | def clearFigure(fig): | |
|
r858 | ||
|
r760 | fig.clf() | |
|
r858 | ||
|
r1075 | ||
|
r201 | def setWinTitle(fig, title): | |
|
r858 | ||
|
r201 | fig.canvas.manager.set_window_title(title) | |
|
r1075 | ||
|
r201 | def setTitle(fig, title): | |
|
r858 | ||
|
r201 | fig.suptitle(title) | |
|
r1075 | ||
|
r502 | def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False): | |
|
r858 | ||
|
r244 | matplotlib.pyplot.ioff() | |
|
r201 | matplotlib.pyplot.figure(fig.number) | |
axes = matplotlib.pyplot.subplot2grid((nrow, ncol), | |||
|
r1075 | (xpos, ypos), | |
colspan=colspan, | |||
rowspan=rowspan, | |||
polar=polar) | |||
|
r858 | ||
|
r244 | matplotlib.pyplot.ion() | |
|
r201 | return axes | |
|
r1075 | ||
|
r201 | def setAxesText(ax, text): | |
|
r858 | ||
|
r201 | ax.annotate(text, | |
|
r1075 | xy=(.1, .99), | |
xycoords='figure fraction', | |||
horizontalalignment='left', | |||
verticalalignment='top', | |||
fontsize=10) | |||
|
r201 | ||
def printLabels(ax, xlabel, ylabel, title): | |||
|
r858 | ||
|
r201 | ax.set_xlabel(xlabel, size=11) | |
ax.set_ylabel(ylabel, size=11) | |||
|
r501 | ax.set_title(title, size=8) | |
|
r858 | ||
|
r1075 | ||
|
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, | |||
|
r1075 | grid=None, color='blue'): | |
|
r204 | """ | |
|
r858 | ||
|
r204 | Input: | |
grid : None, 'both', 'x', 'y' | |||
""" | |||
|
r858 | ||
|
r244 | matplotlib.pyplot.ioff() | |
|
r858 | ||
|
r1075 | ax.set_xlim([xmin, xmax]) | |
ax.set_ylim([ymin, ymax]) | |||
|
r858 | ||
|
r201 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r204 | ###################################################### | |
|
r1075 | if (xmax - xmin) <= 1: | |
xtickspos = numpy.linspace(xmin, xmax, nxticks) | |||
xtickspos = numpy.array([float("%.1f" % i) for i in xtickspos]) | |||
|
r371 | ax.set_xticks(xtickspos) | |
else: | |||
|
r1075 | xtickspos = numpy.arange(nxticks) * \ | |
int((xmax - xmin) / (nxticks)) + int(xmin) | |||
|
r399 | # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin) | |
|
r371 | ax.set_xticks(xtickspos) | |
|
r858 | ||
|
r371 | for tick in ax.get_xticklabels(): | |
tick.set_visible(xtick_visible) | |||
|
r858 | ||
|
r371 | for tick in ax.xaxis.get_major_ticks(): | |
|
r858 | tick.label.set_fontsize(ticksize) | |
|
r204 | ###################################################### | |
for tick in ax.get_yticklabels(): | |||
tick.set_visible(ytick_visible) | |||
|
r858 | ||
|
r204 | for tick in ax.yaxis.get_major_ticks(): | |
tick.label.set_fontsize(ticksize) | |||
|
r858 | ||
|
r474 | ax.plot(x, y, color=color) | |
|
r212 | iplot = ax.lines[-1] | |
|
r858 | ||
|
r204 | ###################################################### | |
|
r212 | if '0.' in matplotlib.__version__[0:2]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r212 | return iplot | |
|
r858 | ||
|
r212 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r212 | return iplot | |
|
r858 | ||
|
r204 | if grid != None: | |
ax.grid(b=True, which='major', axis=grid) | |||
|
r858 | ||
|
r201 | matplotlib.pyplot.tight_layout() | |
|
r858 | ||
|
r244 | matplotlib.pyplot.ion() | |
|
r858 | ||
|
r201 | return iplot | |
|
r1075 | ||
|
r244 | def set_linedata(ax, x, y, idline): | |
|
r858 | ||
|
r1075 | ax.lines[idline].set_data(x, y) | |
|
r858 | ||
|
r201 | def pline(iplot, x, y, xlabel='', ylabel='', title=''): | |
|
r858 | ||
|
r879 | ax = iplot.axes | |
|
r858 | ||
|
r201 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r239 | set_linedata(ax, x, y, idline=0) | |
|
r1075 | ||
|
r239 | def addpline(ax, x, y, color, linestyle, lw): | |
|
r858 | ||
|
r1075 | ax.plot(x, y, color=color, linestyle=linestyle, lw=lw) | |
|
r244 | ||
def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, | |||
|
r1075 | xlabel='', ylabel='', title='', ticksize=9, | |
colormap='jet', cblabel='', cbsize="5%", | |||
|
r244 | XAxisAsTime=False): | |
|
r858 | ||
|
r244 | matplotlib.pyplot.ioff() | |
|
r858 | ||
|
r244 | divider = make_axes_locatable(ax) | |
ax_cb = divider.new_horizontal(size=cbsize, pad=0.05) | |||
fig = ax.get_figure() | |||
fig.add_axes(ax_cb) | |||
|
r858 | ||
|
r1075 | ax.set_xlim([xmin, xmax]) | |
ax.set_ylim([ymin, ymax]) | |||
|
r858 | ||
|
r244 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
z = numpy.ma.masked_invalid(z) | |||
|
r1075 | cmap = matplotlib.pyplot.get_cmap(colormap) | |
|
r956 | cmap.set_bad('black', 1.) | |
|
r1075 | imesh = ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap) | |
cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) | |||
|
r244 | cb.set_label(cblabel) | |
|
r858 | ||
|
r244 | # for tl in ax_cb.get_yticklabels(): | |
# tl.set_visible(True) | |||
|
r858 | ||
|
r244 | for tick in ax.yaxis.get_major_ticks(): | |
tick.label.set_fontsize(ticksize) | |||
|
r858 | ||
|
r244 | for tick in ax.xaxis.get_major_ticks(): | |
|
r858 | tick.label.set_fontsize(ticksize) | |
|
r244 | for tick in cb.ax.get_yticklabels(): | |
tick.set_fontsize(ticksize) | |||
|
r858 | ||
|
r244 | ax_cb.yaxis.tick_right() | |
|
r858 | ||
|
r244 | if '0.' in matplotlib.__version__[0:2]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r244 | return imesh | |
|
r858 | ||
|
r244 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r244 | return imesh | |
|
r858 | ||
|
r244 | matplotlib.pyplot.tight_layout() | |
|
r858 | ||
|
r244 | if XAxisAsTime: | |
|
r858 | ||
|
r1075 | def func(x, pos): return ('%s') % ( | |
datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) | |||
|
r244 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
ax.xaxis.set_major_locator(LinearLocator(7)) | |||
|
r873 | ||
|
r244 | matplotlib.pyplot.ion() | |
return imesh | |||
|
r1075 | ||
|
r244 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): | |
|
r858 | ||
|
r244 | z = z.T | |
|
r879 | ax = imesh.axes | |
|
r244 | printLabels(ax, xlabel, ylabel, title) | |
imesh.set_array(z.ravel()) | |||
|
r1075 | ||
|
r244 | def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): | |
|
r858 | ||
|
r244 | printLabels(ax, xlabel, ylabel, title) | |
|
r873 | ||
|
r1075 | ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, | |
cmap=matplotlib.pyplot.get_cmap(colormap)) | |||
|
r858 | ||
|
r318 | def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): | |
|
r858 | ||
|
r318 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r318 | ax.collections.remove(ax.collections[0]) | |
|
r858 | ||
z = numpy.ma.masked_invalid(z) | |||
|
r927 | ||
|
r1075 | cmap = matplotlib.pyplot.get_cmap(colormap) | |
|
r956 | cmap.set_bad('black', 1.) | |
|
r858 | ||
|
r1075 | ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap) | |
|
r927 | ||
|
r229 | ||
def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, | |||
|
r1075 | ticksize=9, xtick_visible=True, ytick_visible=True, | |
nxticks=4, nyticks=10, | |||
grid=None): | |||
|
r229 | """ | |
|
r858 | ||
|
r229 | Input: | |
grid : None, 'both', 'x', 'y' | |||
""" | |||
|
r858 | ||
|
r244 | matplotlib.pyplot.ioff() | |
|
r858 | ||
|
r229 | lines = ax.plot(x.T, y) | |
|
r240 | leg = ax.legend(lines, legendlabels, loc='upper right') | |
|
r229 | leg.get_frame().set_alpha(0.5) | |
|
r1075 | ax.set_xlim([xmin, xmax]) | |
ax.set_ylim([ymin, ymax]) | |||
|
r229 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r1075 | xtickspos = numpy.arange(nxticks) * \ | |
int((xmax - xmin) / (nxticks)) + int(xmin) | |||
|
r229 | ax.set_xticks(xtickspos) | |
|
r858 | ||
|
r229 | for tick in ax.get_xticklabels(): | |
tick.set_visible(xtick_visible) | |||
|
r858 | ||
|
r229 | for tick in ax.xaxis.get_major_ticks(): | |
|
r858 | tick.label.set_fontsize(ticksize) | |
|
r229 | for tick in ax.get_yticklabels(): | |
tick.set_visible(ytick_visible) | |||
|
r858 | ||
|
r229 | for tick in ax.yaxis.get_major_ticks(): | |
tick.label.set_fontsize(ticksize) | |||
|
r858 | ||
|
r229 | iplot = ax.lines[-1] | |
|
r858 | ||
|
r229 | if '0.' in matplotlib.__version__[0:2]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r229 | return iplot | |
|
r858 | ||
|
r229 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r229 | return iplot | |
|
r858 | ||
|
r229 | if grid != None: | |
ax.grid(b=True, which='major', axis=grid) | |||
|
r858 | ||
|
r229 | matplotlib.pyplot.tight_layout() | |
|
r858 | ||
|
r244 | matplotlib.pyplot.ion() | |
|
r858 | ||
|
r229 | return iplot | |
def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''): | |||
|
r858 | ||
|
r879 | ax = iplot.axes | |
|
r858 | ||
|
r229 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r229 | for i in range(len(ax.lines)): | |
line = ax.lines[i] | |||
|
r1075 | line.set_data(x[i, :], y) | |
|
r240 | ||
|
r858 | ||
|
r1075 | def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, | |
ticksize=9, xtick_visible=True, ytick_visible=True, | |||
nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None", | |||
grid=None, XAxisAsTime=False): | |||
|
r240 | """ | |
|
r858 | ||
|
r240 | Input: | |
grid : None, 'both', 'x', 'y' | |||
""" | |||
|
r858 | ||
|
r244 | matplotlib.pyplot.ioff() | |
|
r858 | ||
|
r337 | # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle) | |
|
r767 | lines = ax.plot(x, y.T) | |
|
r772 | # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \ | |
# handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.) | |||
|
r858 | ||
|
r772 | leg = ax.legend(lines, legendlabels, | |
|
r791 | loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0) | |
|
r858 | ||
|
r1075 | for label in leg.get_texts(): | |
label.set_fontsize(9) | |||
|
r858 | ||
|
r1075 | ax.set_xlim([xmin, xmax]) | |
ax.set_ylim([ymin, ymax]) | |||
|
r240 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r240 | # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) | |
# ax.set_xticks(xtickspos) | |||
|
r858 | ||
|
r240 | for tick in ax.get_xticklabels(): | |
tick.set_visible(xtick_visible) | |||
|
r858 | ||
|
r240 | for tick in ax.xaxis.get_major_ticks(): | |
|
r858 | tick.label.set_fontsize(ticksize) | |
|
r240 | for tick in ax.get_yticklabels(): | |
tick.set_visible(ytick_visible) | |||
|
r858 | ||
|
r240 | for tick in ax.yaxis.get_major_ticks(): | |
tick.label.set_fontsize(ticksize) | |||
|
r858 | ||
|
r240 | iplot = ax.lines[-1] | |
|
r858 | ||
|
r240 | if '0.' in matplotlib.__version__[0:2]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r240 | return iplot | |
|
r858 | ||
|
r240 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r240 | return iplot | |
|
r858 | ||
|
r240 | if grid != None: | |
ax.grid(b=True, which='major', axis=grid) | |||
|
r858 | ||
|
r240 | matplotlib.pyplot.tight_layout() | |
|
r858 | ||
|
r240 | if XAxisAsTime: | |
|
r858 | ||
|
r1075 | def func(x, pos): return ('%s') % ( | |
datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) | |||
|
r240 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
ax.xaxis.set_major_locator(LinearLocator(7)) | |||
|
r858 | ||
|
r244 | matplotlib.pyplot.ion() | |
|
r858 | ||
|
r240 | return iplot | |
|
r1075 | ||
|
r246 | def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''): | |
|
r858 | ||
|
r879 | ax = iplot.axes | |
|
r240 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r240 | for i in range(len(ax.lines)): | |
line = ax.lines[i] | |||
|
r1075 | line.set_data(x, y[i, :]) | |
|
r502 | ||
def createPolar(ax, x, y, | |||
|
r1075 | xlabel='', ylabel='', title='', ticksize=9, | |
colormap='jet', cblabel='', cbsize="5%", | |||
XAxisAsTime=False): | |||
|
r858 | ||
|
r502 | matplotlib.pyplot.ioff() | |
|
r858 | ||
|
r1075 | ax.plot(x, y, 'bo', markersize=5) | |
|
r502 | # ax.set_rmax(90) | |
|
r1075 | ax.set_ylim(0, 90) | |
ax.set_yticks(numpy.arange(0, 90, 20)) | |||
|
r608 | # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11') | |
# ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11') | |||
|
r502 | # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical') | |
|
r879 | ax.yaxis.labelpad = 40 | |
|
r608 | printLabels(ax, xlabel, ylabel, title) | |
|
r502 | iplot = ax.lines[-1] | |
|
r858 | ||
|
r502 | if '0.' in matplotlib.__version__[0:2]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r502 | return iplot | |
|
r858 | ||
|
r502 | if '1.0.' in matplotlib.__version__[0:4]: | |
|
r1167 | print("The matplotlib version has to be updated to 1.1 or newer") | |
|
r502 | return iplot | |
|
r858 | ||
|
r502 | # if grid != None: | |
# ax.grid(b=True, which='major', axis=grid) | |||
|
r858 | ||
|
r502 | matplotlib.pyplot.tight_layout() | |
|
r858 | ||
|
r502 | matplotlib.pyplot.ion() | |
|
r858 | ||
|
r502 | return iplot | |
|
r1075 | ||
|
r502 | def polar(iplot, x, y, xlabel='', ylabel='', title=''): | |
|
r858 | ||
|
r879 | ax = iplot.axes | |
|
r858 | ||
|
r502 | # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11') | |
|
r608 | printLabels(ax, xlabel, ylabel, title) | |
|
r858 | ||
|
r502 | set_linedata(ax, x, y, idline=0) | |
|
r858 | ||
|
r1075 | ||
|
r201 | def draw(fig): | |
|
r858 | ||
|
r201 | if type(fig) == 'int': | |
|
r1167 | raise ValueError("Error drawing: Fig parameter should be a matplotlib figure object figure") | |
|
r858 | ||
|
r404 | fig.canvas.draw() | |
|
r617 | ||
|
r1075 | ||
|
r617 | def pause(interval=0.000001): | |
|
r858 | ||
|
r1167 | matplotlib.pyplot.pause(interval) |