GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — adding option to zoom-in on a section of the plot · jbrillon/quickplotlib@0e301ec

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • quickplotlib.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/quickplotlib.py‎

Copy file name to clipboardExpand all lines: lib/quickplotlib.py
+111-75Lines changed: 111 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,89 @@ def plot_any_axes(func,x,y,lc,mk,ls):
2525
# abstract plotting function to handle any kind of axes
2626
return func(x, y, color=lc, marker=mk, markersize=6, mfc='None', linestyle=ls)
2727
#-----------------------------------------------------
28+
def plot_lines(axes,xdata,ydata,ndata,lnstl,clr,mrkr,black_lines,
29+
which_lines_black,which_lines_only_markers,which_lines_markers,which_lines_dashed,
30+
markers,log_axes,lnstl_input,clr_input,mrkr_input,
31+
error_bars_on_curve_number,yerr_below,yerr_above,
32+
legend_labels_tex,leg_elements_input):
33+
34+
leg_elements = []
35+
ls = lnstl[0] # default line style (ls)
36+
lc = clr[0] # default line color (lc)
37+
mk = 'None'
38+
if(black_lines):
39+
lc = 'k' # set color to black
40+
41+
color_index_shift = 0
42+
43+
for i in range(0,ndata):
44+
if(black_lines):
45+
ls = lnstl[i]
46+
mk = 'None' # reset to default
47+
if(i in which_lines_only_markers):
48+
ls = 'None'
49+
mk = mrkr[i]
50+
elif(i in which_lines_markers):
51+
mk = mrkr[i]
52+
else:
53+
# colour
54+
if(clr_input!=[]):
55+
lc = clr[i]
56+
elif(i in which_lines_black):
57+
lc = 'k'
58+
color_index_shift += 1
59+
else:
60+
lc = clr[i-color_index_shift]
61+
# warning: this color_index_shift could be buggy for when there's multiple desired black lines with colour ones
62+
# -- no issues when only one black line is specified
63+
64+
# linestyle
65+
if(lnstl_input!=[]):
66+
ls = lnstl[i]
67+
elif(i in which_lines_dashed):
68+
ls = "dashed"
69+
elif(i in which_lines_only_markers):
70+
ls = 'None'
71+
else:
72+
ls = 'solid' # default
73+
74+
# markers
75+
if(mrkr_input!=[]):
76+
mk = mrkr[i]
77+
elif(i in which_lines_only_markers):
78+
mk = mrkr[i]
79+
elif(i in which_lines_markers):
80+
mk = mrkr[i]
81+
else:
82+
mk = 'None' # reset to default
83+
84+
x = xdata[i]; y = ydata[i];
85+
if(markers):
86+
mk = mrkr[i]
87+
if(log_axes==None):
88+
if(error_bars_on_curve_number!=[] and i==error_bars_on_curve_number):
89+
yerr = np.array([yerr_below,yerr_above])
90+
fmt_string = lc+mrkr[i]
91+
plt.errorbar(x, y, yerr, fmt=fmt_string, mfc='None')
92+
# plt.errorbar(x, y, yerr, color=lc, marker=mrkr[i], markersize=6, mfc='None', linestyle='None')
93+
else:
94+
plot_any_axes(axes.plot,x,y,lc,mk,ls)
95+
96+
# add legend element
97+
if(legend_labels_tex!=[] and leg_elements_input==[]):
98+
leg_elements.append(Line2D([0],[0], label=legend_labels_tex[i], color=lc, marker=mk, markersize=6, mfc='None', linestyle=ls))
99+
100+
# plot command
101+
if(log_axes==None):
102+
plot_any_axes(axes.plot,x,y,lc,mk,ls)
103+
elif(log_axes=="both"):
104+
plot_any_axes(axes.loglog,x,y,lc,mk,ls)
105+
elif(log_axes=="x"):
106+
plot_any_axes(axes.semilogx,x,y,lc,mk,ls)
107+
elif(log_axes=="y"):
108+
plot_any_axes(axes.semilogy,x,y,lc,mk,ls)
109+
return leg_elements
110+
#-----------------------------------------------------
28111
def plotfxn(xdata=[],ydata=[],ylabel="ydata",xlabel="xdata",
29112
figure_filename="myfig",
30113
figure_filetype="pdf",
@@ -55,7 +138,10 @@ def plotfxn(xdata=[],ydata=[],ylabel="ydata",xlabel="xdata",
55138
legend_location="best",
56139
legend_anchor=[],
57140
second_leg_elements_input=[],
58-
second_leg_anchor=[]):
141+
second_leg_anchor=[],
142+
plot_zoomed_section=False,
143+
x_limits_zoom=[],y_limits_zoom=[],
144+
zoom_box_origin_and_extent=[]):
59145
print("---------------------------------------------")
60146
#-----------------------------------------------------
61147
# Safeguard for when empty data is passed
@@ -123,85 +209,18 @@ def plotfxn(xdata=[],ydata=[],ylabel="ydata",xlabel="xdata",
123209
ax.set_xlim(xlimits)
124210
if(ylimits!=[]):
125211
ax.set_ylim(ylimits)
126-
ls = lnstl[0] # default line style (ls)
127-
lc = clr[0] # default line color (lc)
128-
mk = 'None'
129-
if(black_lines):
130-
lc = 'k' # set color to black
131212

132-
color_index_shift = 0
133-
if(leg_elements_input == []):
134-
leg_elements = []
213+
leg_elements_plt = plot_lines(ax,xdata,ydata,ndata,lnstl,clr,mrkr,black_lines,
214+
which_lines_black,which_lines_only_markers,which_lines_markers,which_lines_dashed,
215+
markers,log_axes,lnstl_input,clr_input,mrkr_input,
216+
error_bars_on_curve_number,yerr_below,yerr_above,
217+
legend_labels_tex,leg_elements_input)
135218

136-
for i in range(0,ndata):
137-
if(black_lines):
138-
ls = lnstl[i]
139-
mk = 'None' # reset to default
140-
if(i in which_lines_only_markers):
141-
ls = 'None'
142-
mk = mrkr[i]
143-
elif(i in which_lines_markers):
144-
mk = mrkr[i]
145-
else:
146-
# colour
147-
if(clr_input!=[]):
148-
lc = clr[i]
149-
elif(i in which_lines_black):
150-
lc = 'k'
151-
color_index_shift += 1
152-
else:
153-
lc = clr[i-color_index_shift]
154-
# warning: this color_index_shift could be buggy for when there's multiple desired black lines with colour ones
155-
# -- no issues when only one black line is specified
156-
157-
# linestyle
158-
if(lnstl_input!=[]):
159-
ls = lnstl[i]
160-
elif(i in which_lines_dashed):
161-
ls = "dashed"
162-
elif(i in which_lines_only_markers):
163-
ls = 'None'
164-
else:
165-
ls = 'solid' # default
166-
167-
# markers
168-
if(mrkr_input!=[]):
169-
mk = mrkr[i]
170-
elif(i in which_lines_only_markers):
171-
mk = mrkr[i]
172-
elif(i in which_lines_markers):
173-
mk = mrkr[i]
174-
else:
175-
mk = 'None' # reset to default
176-
177-
x = xdata[i]; y = ydata[i];
178-
if(markers):
179-
mk = mrkr[i]
180-
if(log_axes==None):
181-
if(error_bars_on_curve_number!=[] and i==error_bars_on_curve_number):
182-
yerr = np.array([yerr_below,yerr_above])
183-
fmt_string = lc+mrkr[i]
184-
plt.errorbar(x, y, yerr, fmt=fmt_string, mfc='None')
185-
# plt.errorbar(x, y, yerr, color=lc, marker=mrkr[i], markersize=6, mfc='None', linestyle='None')
186-
else:
187-
plot_any_axes(plt.plot,x,y,lc,mk,ls)
188-
189-
# add legend element
190-
if(legend_labels_tex!=[] and leg_elements_input==[]):
191-
leg_elements.append(Line2D([0],[0], label=legend_labels_tex[i], color=lc, marker=mk, markersize=6, mfc='None', linestyle=ls))
192-
193-
# plot command
194-
if(log_axes==None):
195-
plot_any_axes(plt.plot,x,y,lc,mk,ls)
196-
elif(log_axes=="both"):
197-
plot_any_axes(plt.loglog,x,y,lc,mk,ls)
198-
elif(log_axes=="x"):
199-
plot_any_axes(plt.semilogx,x,y,lc,mk,ls)
200-
elif(log_axes=="y"):
201-
plot_any_axes(plt.semilogy,x,y,lc,mk,ls)
202219
if(legend_on):
203220
if(leg_elements_input!=[]):
204221
leg_elements=leg_elements_input
222+
else:
223+
leg_elements=leg_elements_plt
205224
if(legend_inside):
206225
if(legend_anchor!=[]):
207226
leg = plt.legend(handles=leg_elements, loc=legend_location, bbox_to_anchor=(legend_anchor[0], legend_anchor[1]), ncol=nlegendcols, shadow=False, fancybox=True, fontsize=legend_fontSize, framealpha=1.0,edgecolor='inherit')
@@ -246,6 +265,23 @@ def plotfxn(xdata=[],ydata=[],ylabel="ydata",xlabel="xdata",
246265
second_leg.get_frame().set_edgecolor('k')
247266
ax.add_artist(second_leg)
248267

268+
if(plot_zoomed_section):
269+
x_limits_zoom
270+
x1, x2, y1, y2 = 7.5, 10.0, 0.010, 0.0135 # subregion of the original image
271+
axins = ax.inset_axes(zoom_box_origin_and_extent,xticklabels=[], yticklabels=[])
272+
axins.set_xlim(x_limits_zoom)
273+
axins.set_ylim(y_limits_zoom)
274+
axins.set_xticks([])
275+
axins.set_yticks([])
276+
277+
dummy = plot_lines(axins,xdata,ydata,ndata,lnstl,clr,mrkr,black_lines,
278+
which_lines_black,which_lines_only_markers,which_lines_markers,which_lines_dashed,
279+
markers,log_axes,lnstl_input,clr_input,mrkr_input,
280+
error_bars_on_curve_number,yerr_below,yerr_above,
281+
legend_labels_tex,leg_elements_input)
282+
283+
ax.indicate_inset_zoom(axins, edgecolor="black")
284+
249285
plt.tight_layout()
250286
print('\t ... Saving figure ...')
251287
plt.savefig(fig_directory+"/"+figure_filename+'.'+figure_filetype,format=figure_filetype,dpi=500)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.