@@ -179,7 +179,7 @@ def _reset_ggplot_legend(bricks):
179
179
tmp_artist .remove ()
180
180
else :
181
181
bricks ._case .artists .remove (bricks ._ggplot_legend )
182
-
182
+
183
183
anchored_box = AnchoredOffsetbox (
184
184
loc = bricks ._ggplot_legend_loc ,
185
185
child = bricks ._ggplot_legend_box ,
@@ -189,9 +189,10 @@ def _reset_ggplot_legend(bricks):
189
189
bbox_transform = bricks ._case .transAxes ,
190
190
borderpad = 0. )
191
191
anchored_box .set_zorder (90.1 )
192
+ anchored_box .set_in_layout (True )
192
193
try :
193
194
bricks ._case .add_artist (anchored_box )
194
- except :
195
+ except Exception as e :
195
196
pass
196
197
bricks ._ggplot_legend = anchored_box
197
198
bricks .case
@@ -239,7 +240,7 @@ def draw_labels(bricks, gori, gcp, figsize):
239
240
pad_x = 4
240
241
else :
241
242
if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
242
- pad_x = 4
243
+ pad_x = 14 + ( get_property ( 'axis_text_x' , 'size' ) - 11 ) * 0.5 + ( get_property ( 'axis_title_x' , 'size' ) - 11 ) * 0.5
243
244
else :
244
245
pad_x = margin .get_as ('t' , 'pt' )
245
246
@@ -249,7 +250,7 @@ def draw_labels(bricks, gori, gcp, figsize):
249
250
pad_y = 4
250
251
else :
251
252
if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
252
- pad_y = 8
253
+ pad_y = 12 + ( get_property ( 'axis_text_y' , 'size' ) - 11 ) * 0.5 + ( get_property ( 'axis_title_y' , 'size' ) - 11 ) * 0.5
253
254
else :
254
255
pad_y = margin .get_as ('r' , 'pt' )
255
256
@@ -276,34 +277,27 @@ def draw_labels(bricks, gori, gcp, figsize):
276
277
277
278
else :
278
279
xlabel = bricks .set_xlabel (labels .x , labelpad = pad_x , va = "top" )
279
- ylabel = bricks .set_ylabel (labels .y , labelpad = pad_y )
280
-
280
+ ylabel = bricks .set_ylabel (labels .y , labelpad = pad_y )
281
+
281
282
if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
282
283
gori .theme ._targets ['axis_title_x' ] = xlabel
283
284
gori .theme ._targets ['axis_title_y' ] = ylabel
284
285
if 'axis_title_x' in gori .theme .themeables :
285
286
gori .theme .themeables ['axis_title_x' ].apply_figure (gori .figure , gori .theme ._targets )
286
287
for ax in gori .axs :
287
- gori .theme .themeables ['axis_title_x' ].apply_ax (ax )
288
+ gori .theme .themeables ['axis_title_x' ].apply_ax (ax )
288
289
289
290
if 'axis_title_y' in gori .theme .themeables :
290
291
gori .theme .themeables ['axis_title_y' ].apply_figure (gori .figure , gori .theme ._targets )
291
292
for ax in gori .axs :
292
- gori .theme .themeables ['axis_title_y' ].apply_ax (ax )
293
-
294
- if bricks ._type == "Bricks" :
295
- xlabel = bricks .case .set_xlabel (labels .x , labelpad = pad_x , va = va )
296
- x ,y = xlabel .get_position ()
297
- xlabel .set_position ([(px1 + px2 ) / 2 , y ])
298
-
299
- ylabel = bricks .case .set_ylabel (labels .y , labelpad = pad_y )
300
- x ,y = ylabel .get_position ()
301
- ylabel .set_position ([x , (py1 + py2 ) / 2 ])
302
-
303
- else :
304
- bricks .set_xlabel (labels .x , labelpad = pad_x , va = va )
305
- bricks .set_ylabel (labels .y , labelpad = pad_y )
306
-
293
+ gori .theme .themeables ['axis_title_y' ].apply_ax (ax )
294
+
295
+ for key in gori .theme .themeables :
296
+ if "legend" in key :
297
+ gori .theme .themeables [key ].apply_figure (gori .figure , gori .theme ._targets )
298
+ for ax in gori .axs :
299
+ gori .theme .themeables [key ].apply_ax (ax )
300
+
307
301
else :
308
302
gori .figure ._themeable ['axis_title_x' ] = xlabel
309
303
gori .figure ._themeable ['axis_title_y' ] = ylabel
@@ -316,11 +310,12 @@ def draw_labels(bricks, gori, gcp, figsize):
316
310
gori .theme .themeables ['axis_title_y' ].apply_figure (gori .figure )
317
311
for ax in gori .axs :
318
312
gori .theme .themeables ['axis_title_y' ].apply (ax )
313
+
314
+ return labels .x , labels .y
319
315
320
316
def draw_legend (bricks , gori , gcp , figsize ):
321
317
get_property = gcp .theme .themeables .property
322
318
legend_box = gcp .guides .build (gcp )
323
-
324
319
if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
325
320
wratio = 1
326
321
hratio = 1
@@ -358,7 +353,7 @@ def draw_legend(bricks, gori, gcp, figsize):
358
353
else :
359
354
loc = 1
360
355
x , y = 0 , 0
361
-
356
+
362
357
if legend_box is None :
363
358
pass
364
359
else :
@@ -372,6 +367,7 @@ def draw_legend(bricks, gori, gcp, figsize):
372
367
borderpad = 0. )
373
368
374
369
anchored_box .set_zorder (90.1 )
370
+ anchored_box .set_in_layout (True )
375
371
bricks .case .add_artist (anchored_box )
376
372
bricks ._ggplot_legend = anchored_box
377
373
bricks ._ggplot_legend_box = legend_box
@@ -475,11 +471,12 @@ def draw_title(bricks, gori, gcp, figsize):
475
471
figure_subplot_wspace_ori = matplotlib .rcParams ["figure.subplot.wspace" ]
476
472
figure_subplot_hspace_ori = matplotlib .rcParams ["figure.subplot.hspace" ]
477
473
figsize_ori = gcp .theme .themeables ['figure_size' ].properties ["value" ]
474
+ if figsize is None :
475
+ figsize = gcp .theme .themeables ['figure_size' ].properties ["value" ]
478
476
matplotlib .rcParams ["figure.subplot.wspace" ] = figure_subplot_wspace_ori / figsize [0 ]
479
477
matplotlib .rcParams ["figure.subplot.hspace" ] = figure_subplot_hspace_ori / figsize [1 ]
480
478
fig , gcp = gcp .draw (return_ggplot = True )
481
- if figsize is None :
482
- figsize = gcp .theme .themeables ['figure_size' ].properties ["value" ]
479
+
483
480
else :
484
481
fig , gcp = gcp .draw (return_ggplot = True )
485
482
_themeable = fig ._themeable
@@ -562,7 +559,6 @@ def draw_title(bricks, gori, gcp, figsize):
562
559
new = themeable .from_class_name
563
560
ggplot .theme .themeables ["figure_size" ] = new ("figure_size" ,(1 ,1 ))
564
561
ggplot .theme .apply ()
565
- #ggplot.figure.set_layout_engine(PlotnineLayoutEngine(ggplot))
566
562
567
563
elif StrictVersion (plotnine_version ) >= StrictVersion ("0.9" ):
568
564
ggplot ._resize_panels ()
@@ -590,7 +586,7 @@ def draw_title(bricks, gori, gcp, figsize):
590
586
ax .change_aspectratio ((figsize [0 ], figsize [1 ]))
591
587
592
588
if StrictVersion (plotnine_version ) >= StrictVersion ("0.9" ):
593
- draw_labels (ax , ggplot , gcp , figsize )
589
+ xl , yl = draw_labels (ax , ggplot , gcp , figsize )
594
590
draw_legend (ax , ggplot , gcp , figsize )
595
591
draw_title (ax , ggplot , gcp , figsize )
596
592
@@ -606,12 +602,12 @@ def draw_title(bricks, gori, gcp, figsize):
606
602
del gcp
607
603
for key in tmp_axes_keys :
608
604
axtmp = _axes_dict [key ]
609
- axtmp .set_position (position_dict [key ])
605
+ axtmp .set_position (position_dict [key ])
610
606
611
607
if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
612
- matplotlib . rcParams [ "figure.subplot.wspace" ] = figure_subplot_wspace_ori
613
- matplotlib . rcParams [ "figure.subplot.hspace" ] = figure_subplot_hspace_ori
614
- return ax
608
+ ax . set_xlabel ( xl )
609
+ ax . set_ylabel ( yl )
610
+ return_obj = ax
615
611
616
612
else :
617
613
width , height = figsize
@@ -628,10 +624,11 @@ def draw_title(bricks, gori, gcp, figsize):
628
624
bricks = expand (bricks , width , height )
629
625
630
626
if StrictVersion (plotnine_version ) >= StrictVersion ("0.9" ):
631
- draw_labels (bricks , ggplot , gcp , figsize )
627
+ xl , yl = draw_labels (bricks , ggplot , gcp , figsize )
632
628
draw_legend (bricks , ggplot , gcp , figsize )
633
629
draw_title (bricks , ggplot , gcp , figsize )
634
-
630
+ pass
631
+
635
632
elif StrictVersion ("0.8" ) <= StrictVersion (plotnine_version ) < StrictVersion ("0.9" ):
636
633
draw_labels (bricks , ggplot , gcp , figsize )
637
634
draw_legend (bricks , ggplot , gcp , figsize )
@@ -644,16 +641,22 @@ def draw_title(bricks, gori, gcp, figsize):
644
641
del gcp
645
642
for key in tmp_axes_keys :
646
643
ax = _axes_dict [key ]
647
- ax .set_position (position_dict [key ])
648
-
644
+ ax .set_position (position_dict [key ])
645
+
649
646
x0 , x1 , y0 , y1 = bricks .get_outer_corner ()
650
647
bricks ._originalsize = (abs (x1 - x0 ), abs (y0 - y1 ))
651
648
bricks .set_originalpositions ()
652
-
653
649
if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
654
- matplotlib .rcParams ["figure.subplot.wspace" ] = figure_subplot_wspace_ori
655
- matplotlib .rcParams ["figure.subplot.hspace" ] = figure_subplot_hspace_ori
656
- return bricks
650
+ bricks .case .set_xlabel (xl )
651
+ bricks .case .set_ylabel (yl )
652
+ return_obj = bricks
653
+
654
+ if StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
655
+ matplotlib .rcParams ["figure.subplot.wspace" ] = figure_subplot_wspace_ori
656
+ matplotlib .rcParams ["figure.subplot.hspace" ] = figure_subplot_hspace_ori
657
+ return_obj .savefig (_ggplot = True )
658
+
659
+ return return_obj
657
660
658
661
def overwrite_axisgrid ():
659
662
"""
@@ -2366,7 +2369,7 @@ def get_middle_corner(self, labels=None):
2366
2369
for key in self .bricks_dict :
2367
2370
if key in labels :
2368
2371
ax = self .bricks_dict [key ]
2369
- px0 , px1 , py0 , py1 = ax .get_middle_corner ()
2372
+ px0 , px1 , py0 , py1 = ax .get_middle_corner ()
2370
2373
x0_list .append (px0 )
2371
2374
x1_list .append (px1 )
2372
2375
y0_list .append (py0 )
@@ -2420,7 +2423,7 @@ def get_outer_corner(self):
2420
2423
2421
2424
return min (x0_list ), max (x1_list ), min (y0_list ), max (y1_list )
2422
2425
2423
- def savefig (self , fname = None , transparent = None , quick = True , ** kwargs ):
2426
+ def savefig (self , fname = None , transparent = None , quick = True , _ggplot = False , ** kwargs ):
2424
2427
"""
2425
2428
2426
2429
Save figure.
@@ -2443,6 +2446,7 @@ def savefig(self, fname=None, transparent=None, quick=True, **kwargs):
2443
2446
_axes_dict [":" .join (case_label .split (":" )[1 :])].case
2444
2447
2445
2448
global param
2449
+ global _basefigure
2446
2450
global _removed_axes
2447
2451
if quick == False :
2448
2452
self .case
@@ -2483,7 +2487,11 @@ def savefig(self, fname=None, transparent=None, quick=True, **kwargs):
2483
2487
kwargs .setdefault ('bbox_inches' , 'tight' )
2484
2488
kwargs .setdefault ('dpi' , param ['dpi' ])
2485
2489
fig .savefig (fname , transparent = transparent , ** kwargs )
2486
-
2490
+ else :
2491
+ if _ggplot == True :
2492
+ bytefig = io .BytesIO ()
2493
+ _basefigure .savefig (bytefig , format = "pdf" )
2494
+
2487
2495
return fig
2488
2496
2489
2497
def __or__ (self , other ):
@@ -2859,7 +2867,7 @@ def get_outer_corner(self, labes=None):
2859
2867
self ._outer_flag = True
2860
2868
return self ._outer_corner
2861
2869
2862
- def savefig (self , fname = None , transparent = None , quick = True , ** kwargs ):
2870
+ def savefig (self , fname = None , transparent = None , quick = True , _ggplot = False , ** kwargs ):
2863
2871
"""
2864
2872
2865
2873
Save figure.
@@ -2906,11 +2914,15 @@ def savefig(self, fname=None, transparent=None, quick=True, **kwargs):
2906
2914
else :
2907
2915
ax .remove ()
2908
2916
_removed_axes [ax .get_label ()] = ax
2909
-
2910
- if fname is not None :
2917
+ if fname is not None :
2911
2918
kwargs .setdefault ('bbox_inches' , 'tight' )
2912
2919
kwargs .setdefault ('dpi' , param ['dpi' ])
2913
2920
fig .savefig (fname , transparent = transparent , ** kwargs )
2921
+ else :
2922
+ if _ggplot == True :
2923
+ bytefig = io .BytesIO ()
2924
+ _basefigure .savefig (bytefig , format = "pdf" )
2925
+
2914
2926
return fig
2915
2927
2916
2928
def change_plotsize (self , new_size ):
0 commit comments