Skip to content

Commit

Permalink
Merge pull request #2092 from thatguy7/develop
Browse files Browse the repository at this point in the history
improve plotly draw_traces figure aspect_ratio calculation
  • Loading branch information
SteffenMeinecke authored Nov 29, 2023
2 parents bdc7de8 + 3df71e6 commit 094c588
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions pandapower/plotting/plotly/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,15 +1094,25 @@ def draw_traces(traces, on_map=False, map_style='basic', showlegend=True, figsiz
for trace in traces:
xs += trace.get('x') or trace['lon']
ys += trace.get('y') or trace['lat']
x_dropna = pd.Series(xs).dropna()
y_dropna = pd.Series(ys).dropna()
xrange = x_dropna.max() - x_dropna.min()
yrange = y_dropna.max() - y_dropna.min()
ratio = xrange / yrange
if ratio < 1:
aspectratio = (ratio, 1.)
xs_arr = np.array(xs)
ys_arr = np.array(ys)
xrange = np.nanmax(xs_arr) - np.nanmin(xs_arr)
yrange = np.nanmax(ys_arr) - np.nanmin(ys_arr)

# the ratio only makes sense, if xrange and yrange != 0
if xrange == 0 and yrange == 0:
aspectratio = (1, 1)
elif xrange == 0:
aspectratio = (0.35, 1)
elif yrange == 0:
aspectratio = (1, 0.35)

else:
aspectratio = (1., 1 / ratio)
ratio = xrange / yrange
if ratio < 1:
aspectratio = (ratio, 1.)
else:
aspectratio = (1., 1 / ratio)

aspectratio = np.array(aspectratio) / max(aspectratio)
fig['layout']['width'], fig['layout']['height'] = ([ar * figsize * 700 for ar in aspectratio])
Expand Down

0 comments on commit 094c588

Please sign in to comment.