Skip to content

Commit 2046274

Browse files
Merge pull request #77 from inwoo-park/plot
Update plot functions in Python: plot_gridded.py, applyoptions.py, plot_contour.py.
2 parents 3eda10a + 396fc0b commit 2046274

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

src/m/plot/applyoptions.py

100755100644
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
except ImportError:
77
print("could not import pyplot, matplotlib has not been installed, no plotting capabilities enabled")
88
import numpy as np
9+
import warnings
910

1011
from expdisp import expdisp
1112
from plot_contour import plot_contour
@@ -229,8 +230,20 @@ def applyoptions(md, data, options, fig, axgrid, gridindex):
229230
if options.exist('log'):
230231
formatter = mpl.ticker.LogFormatterSciNotation(base=options.getfieldvalue('log'))
231232

232-
cb = mpl.colorbar.ColorbarBase(ax.cax, cmap=cmap, norm=norm, extend=extend,
233-
format=formatter)
233+
try:
234+
#NOTE: axis made by ImageGrid contains "cax". If axis generated by "plt.subplots()", we have to manually generate cax...
235+
# type(ax) == mpl_toolkits.axes_grid1.mpl_axes.Axes
236+
cb = mpl.colorbar.ColorbarBase(ax.cax, cmap=cmap, norm=norm, extend=extend,
237+
format=formatter)
238+
except:
239+
from mpl_toolkits.axes_grid1 import make_axes_locatable
240+
driver = make_axes_locatable(ax)
241+
cax = driver.append_axes(options.getfieldvalue('colorbarpos','right'),
242+
size=options.getfieldvalue('colorbarsize','5%'),
243+
pad=options.getfieldvalue('colorbarpad',0.025))
244+
cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm, extend=extend,
245+
format=formatter)
246+
234247
if options.exist('alpha'):
235248
cb.set_alpha(options.getfieldvalue('alpha'))
236249
if options.exist('colorbarnumticks'):
@@ -293,7 +306,15 @@ def applyoptions(md, data, options, fig, axgrid, gridindex):
293306
# }}}
294307
# {{{ north arrow TODO
295308
# }}}
296-
# {{{ scale ruler TODO
309+
# {{{ scale ruler
310+
#TODO: The current version for scaleruler relies on "matplotlib-scalebar"; however, should this section be moved to a standalone "scaleruler" module, similar to the MATLAB version?
311+
if options.exist('scaleruler'):
312+
try:
313+
from matplotlib_scalebar.scalebar import ScaleBar
314+
scalebar = ScaleBar(1,'m',location=options.getfieldvalue('scalerulerlocation','lower right'))
315+
ax.add_artist(scalebar)
316+
except:
317+
warnings.warn('We cannot draw scalebar. Install "matplotlib-scalebar" using "pip install matplotlib-scalebar".')
297318
# }}}
298319
# {{{ streamlines TOFIX
299320
if options.exist('streamlines'):
@@ -310,6 +331,9 @@ def applyoptions(md, data, options, fig, axgrid, gridindex):
310331
# {{{ flat edges of a partition TODO
311332
# }}}
312333
# {{{ scatter TODO
334+
if options.exist('scatter'):
335+
data=getfieldvalue(options,'scatter')
336+
plot_scatter(data[:,0],data[:,1],data[:3],options)
313337
# }}}
314338
# {{{ backgroundcolor TODO
315339
# }}}

src/m/plot/plot_contour.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ def plot_contour(md, datain, options, ax):
3636
linestyles = options.getfieldvalue('contourlinestyles', '-')
3737
linewidths = options.getfieldvalue('contourlinewidths', 1)
3838

39-
ax.tricontour(x, y, elements, data, levels, colors=colors, norm=norm, linestyles=linestyles, linewidths=linewidths)
39+
contour=ax.tricontour(x, y, elements, data, levels, colors=colors, norm=norm, linestyles=linestyles, linewidths=linewidths)
40+
41+
#labels?
42+
if options.getfieldvalue('contourticks','off') == 'on':
43+
print("Catch contour ticks")
44+
ax.clabel(contour,fmt=options.getfieldvalue('contourclabelfmt','%.2f'),
45+
fontsize=options.getfieldvalue('contourclabelsize',10))

src/m/plot/plot_gridded.py

100755100644
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import matplotlib.colors
44
import warnings
55
import numpy as np
6+
from numpy import ma
67
from InterpFromMeshToGrid import InterpFromMeshToGrid
78
from processmesh import processmesh
89
from processdata import processdata
@@ -58,6 +59,11 @@ def plot_gridded(md,data,options,fig,axgrid,gridindex):
5859
ny = int(np.diff(ylim)[0]/posty)+1
5960
x_m = np.linspace(xlim[0],xlim[1],nx)
6061
y_m = np.linspace(ylim[0],ylim[1],ny)
62+
63+
#Tricky part in treating masked value.
64+
if isinstance(data,np.ma.core.MaskedArray):
65+
data[data.mask] = np.nan
66+
6167
#NOTE: Tricky part for elements in interpolation.
6268
data_grid=InterpFromMeshToGrid(elements+1,x,y,data,x_m,y_m,np.nan)
6369
data_grid_save = copy.deepcopy(data_grid)
@@ -81,16 +87,24 @@ def plot_gridded(md,data,options,fig,axgrid,gridindex):
8187
else:
8288
data_min=np.nanmin(data_grid[:])
8389
data_max=np.nanmax(data_grid[:])
90+
caxis_opt=[data_min, data_max] # Back-up caxis for "applyoptions".
8491

8592
#TODO: Select plot area
8693
#subplotmodel(plotlines,plotcols,i,options);
8794

8895
#shading interp;
89-
options.addfielddefault('colormap',plt.cm.viridis)
96+
options.addfielddefault('colormap',plt.cm.turbo)
97+
#options.addfielddefault('colormap',plt.cm.viridis)
9098
cmap = getcolormap(copy.deepcopy(options))
9199
#TODO: Matlab version
92100
#image_rgb = ind2rgb(uint16((data_grid - data_min)*(length(map)/(data_max-data_min))),cmap);
93101
#NOTE: Python version
102+
if options.exist('log'):
103+
#NOTE: Tricky part for log scale figure. "log" scale option does not rely on "processdata.py" function.
104+
data_grid=np.log(data_grid)/np.log(options.getfieldvalue('log'))
105+
data_min =np.log(data_min)/np.log(options.getfieldvalue('log'))
106+
data_max =np.log(data_max)/np.log(options.getfieldvalue('log'))
107+
94108
if isinstance(cmap,matplotlib.colors.ListedColormap):
95109
data_norm = (data_grid-data_min)/(data_max-data_min)
96110
image_rgb = cmap(data_norm)
@@ -145,12 +159,12 @@ def plot_gridded(md,data,options,fig,axgrid,gridindex):
145159
#patch('Faces',[A B C],'Vertices', [x y z],'FaceVertexCData',data_grid(1)*ones(size(x)),'FaceColor','none','EdgeColor',getfieldvalue(options,'edgecolor'));
146160
ax.triplot(x,y,triangles=elements,
147161
color=options.getfieldvalue('edgecolor'),
148-
linewdith=options.getfieldvalue('linewidth',1),
162+
linewidth=options.getfieldvalue('linewidth',1),
149163
)
150164

151165
#Apply options
152166
if (not np.isnan(data_min)) & (not np.isinf(data_min)):
153-
options.changefieldvalue('caxis',[data_min, data_max]) # force caxis so that the colorbar is ready
167+
options.changefieldvalue('caxis',caxis_opt) # force caxis so that the colorbar is ready
154168
options.addfielddefault('axis','xy equal'); # default axis
155169
applyoptions(md,data,options,fig,axgrid,gridindex)
156170

src/m/plot/plot_landsat.py

100755100644
File mode changed.

src/m/plot/plot_manager.py

100755100644
File mode changed.

src/m/plot/plotmodel.py

100755100644
File mode changed.

src/m/plot/processdata.py

100755100644
File mode changed.

0 commit comments

Comments
 (0)