Skip to content

Commit a314a93

Browse files
authored
Add files via upload
Small code adaptation to define symbol plot sizes according to the data input magnitude range.
1 parent c0035d6 commit a314a93

File tree

1 file changed

+120
-30
lines changed

1 file changed

+120
-30
lines changed

plotFMC.py

+120-30
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
# version 1.6
20+
# Diamond source-type diagram included
21+
# Some plot adjustments were done
22+
# version 1.9
23+
# The scale of the symbols now is adjusted to the magnitude range of the input data
1824

1925
import matplotlib.pyplot as plt
20-
from numpy import zeros, sqrt, arcsin, pi, sin
26+
from numpy import zeros, sqrt, arcsin, pi, sin, squeeze, round
2127
from functionsFMC import kave
2228

2329
plt.rc('pdf', fonttype=3)
@@ -169,36 +175,38 @@ def baseplot(spacing, plotname):
169175

170176

171177
def circles(X, Y, size, color, plotname, label, spacing):
172-
178+
# working on it
179+
# computes min and max size to provide adecuate symbol scaling
180+
minval = min(size)
181+
maxval = max(size)
182+
minscale=20
183+
maxscale=140
184+
symb_size = [(minscale + (valor - minval) * (maxscale - minscale) / (maxval - minval)) for valor in size]
185+
173186
fig = baseplot(spacing, plotname)
174187
if str(color) == 'white':
175-
sc = plt.scatter(X, Y, s=size, c=color, alpha=0.7, linewidth=1.5, edgecolors='black')
188+
sc = plt.scatter(X, Y, s=symb_size, c=color, alpha=0.7, linewidth=0.5, edgecolors='black')
176189
else:
177190
sc = plt.scatter(
178191
X,
179192
Y,
180-
s=size,
193+
s=symb_size,
181194
c=color, # AQUI HAY UN PROBLEMA AL UTILIZAR NUMEROS EN ID PARA COLOREAR PROBLEMA EN LA FUNCION COLOR DE matplotlib 3
182195
alpha=0.7,
183-
linewidth=1.5,
184-
edgecolors='black',
196+
linewidth=0.5,
197+
edgecolors='black',
185198
cmap='plasma_r')
186199
cbar = plt.colorbar(sc, shrink=0.5)
187200
cbar.set_label(label)
188201
# legend
189-
plt.scatter(0.3, 0.9, s=16, c='white', linewidth=1.5, edgecolors='black')
190-
plt.scatter(0.4, 0.9, s=25, c='white', linewidth=1.5, edgecolors='black')
191-
plt.scatter(0.5, 0.9, s=36, c='white', linewidth=1.5, edgecolors='black')
192-
plt.scatter(0.6, 0.9, s=49, c='white', linewidth=1.5, edgecolors='black')
193-
plt.scatter(0.7, 0.9, s=64, c='white', linewidth=1.5, edgecolors='black')
194-
plt.scatter(0.8, 0.9, s=81, c='white', linewidth=1.5, edgecolors='black')
195-
plt.text(0.3, .95, '4', fontsize=10)
196-
plt.text(0.4, .95, '5', fontsize=10)
197-
plt.text(0.5, .95, '6', fontsize=10)
198-
plt.text(0.6, .95, '7', fontsize=10)
199-
plt.text(0.7, .95, '8', fontsize=10)
200-
plt.text(0.8, .95, '9', fontsize=10)
201-
plt.text(0.85, .95, 'Mw', fontsize=10)
202+
plt.scatter(0.4, 0.9, s=20, c='white', linewidth=0.5, edgecolors='black')
203+
plt.scatter(0.5, 0.9, s=50, c='white', linewidth=0.5, edgecolors='black')
204+
plt.scatter(0.6, 0.9, s=80, c='white', linewidth=0.5, edgecolors='black')
205+
plt.scatter(0.7, 0.9, s=110, c='white', linewidth=0.5, edgecolors='black')
206+
plt.scatter(0.8, 0.9, s=140, c='white', linewidth=0.5, edgecolors='black')
207+
plt.text(0.4, .95, str(round(minval,1)).strip("'[]'"), fontsize=9, ha='center')
208+
plt.text(0.8, .95, str(round(maxval,1)).strip("'[]'"), fontsize=9, ha='center')
209+
plt.text(0.9, .95, 'Mw', fontsize=9)
202210
return fig
203211

204212

@@ -214,17 +222,17 @@ def annot(X, Y, size, color, plotname, label, annots, lab_param, spacing):
214222
verticalalignment='bottom',
215223
rotation=30,
216224
size='x-small')
217-
plt.text(
218-
1.4,
219-
-0.75,
220-
'Text label:\n' + str(
221-
lab_param).strip(
222-
"'[]'").replace(
223-
"_",
224-
" "),
225-
fontsize=10,
226-
horizontalalignment='right',
227-
verticalalignment='top')
225+
plt.text(
226+
1.05,
227+
-0.75,
228+
'Text label:\n' + str(
229+
lab_param).strip(
230+
"'[]'").replace(
231+
"_",
232+
" "),
233+
fontsize=10,
234+
horizontalalignment='center',
235+
verticalalignment='top')
228236

229237
return fig
230238

@@ -261,3 +269,85 @@ def grids(spacing, plotname):
261269
P = arcsin(sqrt((1 - (a / 50.0)) * compl)) / (pi / 180)
262270
X[0][a], Y[0][a] = kave(T, B, P)
263271
plt.plot(X[0], Y[0], color='gray', linewidth=0.5, linestyle='--')
272+
273+
# Source type diagram, diamond skeewed from Hudson et al. (1989)
274+
def diamond_base(plotname):
275+
fig = plt.figure()
276+
plt.axes().set_aspect('equal')
277+
278+
plt.plot([0,1.3333,0,-1.3333,0],[1,0.3333,-1,-0.3333,1],linewidth=2,color='black') # diagram limits
279+
plt.plot([-1,1],[0,0],linewidth=1,color='black') # horizontal line
280+
plt.plot([0,0],[-1,1],linewidth=1,color='black') # vertical line
281+
plt.plot([-1.3333,1.3333],[-0.3333,0.3333],linewidth=1,color='black',linestyle='--') # diagonal line
282+
283+
plt.text(0, -1.4, plotname, horizontalalignment='center', fontsize=16) # figure title
284+
# Labels
285+
plt.text(0,-1.1,'Implosion', horizontalalignment='center', fontsize=10)
286+
plt.text(0,1.05,'Explosion', horizontalalignment='center', fontsize=10)
287+
plt.text(1.05,0,'CLVD (-)', horizontalalignment='left', verticalalignment='center', fontsize=10)
288+
plt.text(-1.05,0,'CLVD', horizontalalignment='right', verticalalignment='center', fontsize=10)
289+
290+
plt.axis('off')
291+
292+
return fig
293+
294+
def diamond_circles(u, v, size, color, plotname, label):
295+
# computes min and max size to provide adecuate symbol scaling
296+
minval = min(size)
297+
maxval = max(size)
298+
minscale=20
299+
maxscale=100
300+
symb_size = [(minscale + (valor - minval) * (maxscale - minscale) / (maxval - minval)) for valor in size]
301+
302+
fig = diamond_base(plotname)
303+
if str(color) == 'white':
304+
sc = plt.scatter(u, v, s=symb_size, c=color, alpha=0.7, linewidth=0.5, edgecolors='black')
305+
else:
306+
sc = plt.scatter(
307+
u,
308+
v,
309+
s=symb_size,
310+
c=color, # AQUI HAY UN PROBLEMA AL UTILIZAR NUMEROS EN ID PARA COLOREAR PROBLEMA EN LA FUNCION COLOR DE matplotlib 3
311+
alpha=0.7,
312+
linewidth=0.5,
313+
edgecolors='black',
314+
cmap='plasma_r')
315+
cbar = plt.colorbar(sc, shrink=0.5)
316+
cbar.set_label(label)
317+
318+
# legend
319+
plt.scatter(0.4, 0.9, s=20, c='white', linewidth=0.5, edgecolors='black')
320+
plt.scatter(0.5, 0.9, s=40, c='white', linewidth=0.5, edgecolors='black')
321+
plt.scatter(0.6, 0.9, s=60, c='white', linewidth=0.5, edgecolors='black')
322+
plt.scatter(0.7, 0.9, s=80, c='white', linewidth=0.5, edgecolors='black')
323+
plt.scatter(0.8, 0.9, s=100, c='white', linewidth=0.5, edgecolors='black')
324+
plt.text(0.4, .97, str(round(minval,1)).strip("'[]'"), fontsize=9, ha='center')
325+
plt.text(0.8, .97, str(round(maxval,1)).strip("'[]'"), fontsize=9, ha='center')
326+
plt.text(0.9, .97, 'Mw', fontsize=9)
327+
return fig
328+
329+
def diamond_annot(X, Y, size, color, plotname, label, annots, lab_param):
330+
331+
fig = diamond_circles(X, Y, size, color, plotname, label)
332+
for i, txt in enumerate(annots):
333+
plt.annotate(
334+
str(txt).strip(".'[]'"),
335+
(X[i] + 0.01,
336+
Y[i] + 0.01),
337+
horizontalalignment='left',
338+
verticalalignment='bottom',
339+
rotation=30,
340+
size='x-small')
341+
plt.text(
342+
1.7,
343+
-1.25,
344+
'Text label:\n' + str(
345+
lab_param).strip(
346+
"'[]'").replace(
347+
"_",
348+
" "),
349+
fontsize=10,
350+
horizontalalignment='center',
351+
verticalalignment='top')
352+
353+
return fig

0 commit comments

Comments
 (0)