8
8
import pickle
9
9
import warnings
10
10
from math import log10 , floor
11
- from distutils .version import LooseVersion , StrictVersion
11
+ from packaging .version import Version
12
+ #from distutils.version import LooseVersion, StrictVersion
12
13
13
14
import matplotlib
14
15
import matplotlib .font_manager as fm
@@ -173,7 +174,7 @@ def _reset_ggplot_legend(bricks):
173
174
"""
174
175
175
176
if "_ggplot_legend" in bricks .__dict__ and bricks ._ggplot_legend is not None :
176
- if (matplotlib .__version__ ) >= StrictVersion ("3.7" ):
177
+ if Version (matplotlib .__version__ ) >= Version ("3.7" ):
177
178
for tmp_artist in bricks ._case .artists :
178
179
if tmp_artist == bricks ._ggplot_legend :
179
180
tmp_artist .remove ()
@@ -199,7 +200,10 @@ def _reset_ggplot_legend(bricks):
199
200
200
201
if "_seaborn_legend" in bricks .__dict__ and bricks ._seaborn_legend is not None :
201
202
old_legend = bricks ._case .legend_
202
- handles = old_legend .legendHandles
203
+ if Version (matplotlib .__version__ ) >= Version ("3.8" ):
204
+ handles = old_legend .legend_handles
205
+ else :
206
+ handles = old_legend .legendHandles
203
207
labels = [t .get_text () for t in old_legend .get_texts ()]
204
208
title = old_legend .get_title ().get_text ()
205
209
if "bbox_to_anchor" in bricks ._seaborn_legend [0 ]:
@@ -210,7 +214,7 @@ def _reset_ggplot_legend(bricks):
210
214
pass
211
215
212
216
def overwrite_plotnine ():
213
- if StrictVersion (plotnine .__version__ ) >= StrictVersion ("0.13" ):
217
+ if Version (plotnine .__version__ ) >= Version ("0.13" ):
214
218
plotnine .ggplot .draw = mp9 .newdraw
215
219
plotnine .facets .facet .setup = mp9 .setup
216
220
plotnine .facets .facet .make_figure = mp9 .make_figure
@@ -243,7 +247,7 @@ def draw_labels(bricks, gori, gcp, figsize):
243
247
except KeyError :
244
248
pad_x = 4
245
249
else :
246
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
250
+ if Version ( plotnine . __version__ ) >= Version ("0.12" ):
247
251
pad_x = 16 + (get_property ('axis_text_x' , 'size' ) - 11 ) * 0.5 + (get_property ('axis_title_x' , 'size' ) - 11 ) * 0.5
248
252
else :
249
253
pad_x = margin .get_as ('t' , 'pt' )
@@ -253,7 +257,7 @@ def draw_labels(bricks, gori, gcp, figsize):
253
257
except KeyError :
254
258
pad_y = 4
255
259
else :
256
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
260
+ if Version ( plotnine . __version__ ) >= Version ("0.12" ):
257
261
pad_y = 13 + (get_property ('axis_text_y' , 'size' ) - 11 ) * 0.5 + (get_property ('axis_title_y' , 'size' ) - 11 ) * 0.5
258
262
else :
259
263
pad_y = margin .get_as ('r' , 'pt' )
@@ -283,7 +287,7 @@ def draw_labels(bricks, gori, gcp, figsize):
283
287
xlabel = bricks .set_xlabel (labels .x , labelpad = pad_x , va = "top" )
284
288
ylabel = bricks .set_ylabel (labels .y , labelpad = pad_y )
285
289
286
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
290
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
287
291
gori .theme .targets .axis_title_x = xlabel
288
292
gori .theme .targets .axis_title_y = ylabel
289
293
if 'axis_title_x' in gori .theme .themeables :
@@ -302,8 +306,7 @@ def draw_labels(bricks, gori, gcp, figsize):
302
306
for ax in gori .axs :
303
307
gori .theme .themeables [key ].apply_ax (ax )
304
308
305
-
306
- elif StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
309
+ elif Version (plotnine .__version__ ) >= Version ("0.12" ):
307
310
gori .theme ._targets ['axis_title_x' ] = xlabel
308
311
gori .theme ._targets ['axis_title_y' ] = ylabel
309
312
if 'axis_title_x' in gori .theme .themeables :
@@ -340,15 +343,15 @@ def draw_labels(bricks, gori, gcp, figsize):
340
343
def draw_legend (bricks , gori , gcp , figsize ):
341
344
get_property = gcp .theme .themeables .property
342
345
#print(get_property)
343
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
346
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
344
347
legend = gcp .guides ._build ()
345
348
if len (legend ) == 0 :
346
349
return None
347
350
legend_box = legend [0 ].draw ()
348
351
gcp .guides ._apply_guide_themes (legend )
349
352
wratio = 1
350
353
hratio = 1
351
- elif StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
354
+ elif Version ( plotnine . __version__ ) >= Version ("0.12" ):
352
355
legend_box = gcp .guides .build (gcp )
353
356
wratio = 1
354
357
hratio = 1
@@ -363,7 +366,7 @@ def draw_legend(bricks, gori, gcp, figsize):
363
366
except KeyError :
364
367
spacing = 0.1
365
368
366
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
369
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
367
370
spacing = gcp .theme .getp ('legend_box_spacing' )
368
371
position = gcp .theme .getp ("legend_position" )
369
372
else :
@@ -413,7 +416,7 @@ def draw_legend(bricks, gori, gcp, figsize):
413
416
bricks ._ggplot_legend_loc = loc
414
417
bricks ._ggplot_legend_x = x
415
418
bricks ._ggplot_legend_y = y
416
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
419
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
417
420
#from plotnine.themes.theme import themeable, theme_update
418
421
#gori.theme.themeables['legend_background'] = themeable.from_class_name('legend_background', anchored_box)
419
422
for key in gori .theme .themeables :
@@ -422,8 +425,8 @@ def draw_legend(bricks, gori, gcp, figsize):
422
425
gori .theme .themeables [key ].apply_figure (gori .figure , gori .theme .targets )
423
426
for ax in gori .axs :
424
427
gori .theme .themeables [key ].apply_ax (ax )
425
-
426
- elif StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
428
+
429
+ elif Version ( plotnine . __version__ ) >= Version ("0.12" ):
427
430
gori .theme ._targets ['legend_background' ] = anchored_box
428
431
for key in gori .theme .themeables :
429
432
if "legend" in key :
@@ -491,17 +494,18 @@ def draw_title(bricks, gori, gcp, figsize):
491
494
else :
492
495
text = bricks ._case .set_title (title , pad = pad , fontsize = fontsize , x = x , ha = ha , va = va )
493
496
494
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
497
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
495
498
gori .theme .targets .plot_title = text
496
499
gori .theme .themeables ['plot_title' ].apply_figure (gori .figure , gori .theme .targets )
497
500
for ax in gori .axs :
498
501
gori .theme .themeables ['plot_title' ].apply_ax (ax )
499
-
500
- elif StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
502
+
503
+ elif Version ( plotnine . __version__ ) >= Version ("0.12" ):
501
504
gori .theme ._targets ['plot_title' ] = text
502
505
gori .theme .themeables ['plot_title' ].apply_figure (gori .figure , gori .theme ._targets )
503
506
for ax in gori .axs :
504
507
gori .theme .themeables ['plot_title' ].apply_ax (ax )
508
+
505
509
else :
506
510
gori .figure ._themeable ['plot_title' ] = text
507
511
gori .theme .themeables ['plot_title' ].apply_figure (gori .figure )
@@ -520,7 +524,7 @@ def draw_title(bricks, gori, gcp, figsize):
520
524
position_dict [key ] = axtmp .get_position ()
521
525
522
526
gcp = copy .deepcopy (ggplot )
523
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
527
+ if Version ( plotnine . __version__ ) >= Version ("0.12" ):
524
528
figure_subplot_wspace_ori = matplotlib .rcParams ["figure.subplot.wspace" ]
525
529
figure_subplot_hspace_ori = matplotlib .rcParams ["figure.subplot.hspace" ]
526
530
figsize_ori = gcp .theme .themeables ['figure_size' ].properties ["value" ]
@@ -544,8 +548,8 @@ def draw_title(bricks, gori, gcp, figsize):
544
548
strips = []
545
549
546
550
ggplot ._build ()
547
-
548
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
551
+
552
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
549
553
ggplot .facet .make_figure = mp9 .make_figure
550
554
fig , axs = plotnine .facets .facet .setup (ggplot .facet , _basefigure , ggplot )
551
555
else :
@@ -557,10 +561,10 @@ def draw_title(bricks, gori, gcp, figsize):
557
561
ggplot .figure = _basefigure
558
562
ggplot .axs = axs
559
563
560
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
564
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
561
565
ggplot .theme = gcp .theme
562
-
563
- elif StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
566
+
567
+ elif Version ( plotnine . __version__ ) >= Version ("0.12" ):
564
568
ggplot .theme = gcp .theme
565
569
ggplot .theme ._targets = gcp .theme ._targets
566
570
@@ -582,21 +586,20 @@ def draw_title(bricks, gori, gcp, figsize):
582
586
ggplot .axs [i ].spines [bar ].set_ec (gcp .axs [i ].spines [bar ].get_ec ())
583
587
ggplot .axs [i ].spines [bar ].set_visible (gcp .axs [i ].spines [bar ].get_visible ())
584
588
585
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
589
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
586
590
ggplot .theme .setup (ggplot )
587
591
else :
588
592
ggplot ._setup_parameters ()
589
593
ggplot .facet .strips .generate ()
590
594
591
595
for i in range (len (ggplot .facet .strips )):
592
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
596
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
593
597
ggplot .facet .strips [i ].label_info = strips [i ].label_info
594
598
ggplot .facet .strips [i ].layout_info = strips [i ].layout_info
595
599
ggplot .facet .strips [i ].theme = strips [i ].theme
596
600
ggplot .facet .strips [i ].position = strips [i ].position
597
601
ggplot .facet .strips [i ].figure = ggplot .facet .strips [i ].ax
598
-
599
- elif StrictVersion (plotnine_version ) >= StrictVersion ("0.12" ):
602
+ elif Version (plotnine .__version__ ) >= Version ("0.12" ):
600
603
ggplot .facet .strips [i ].position = strips [i ].draw_info .position
601
604
ggplot .facet .strips [i ].draw_info .box_height = strips [i ].draw_info .box_height
602
605
ggplot .facet .strips [i ].draw_info .box_width = strips [i ].draw_info .box_width
@@ -620,14 +623,14 @@ def draw_title(bricks, gori, gcp, figsize):
620
623
ggplot .facet .strips [i ].info .y = strips [i ].info .y
621
624
622
625
#Drawing
623
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12 " ):
626
+ if Version ( plotnine . __version__ ) >= Version ("0.13 " ):
624
627
from plotnine ._mpl .layout_engine import PlotnineLayoutEngine
625
628
from plotnine .themes .themeable import Themeables , themeable
626
629
for i , l in enumerate (ggplot .layers , start = 1 ):
627
630
l .zorder = i + 10
628
631
l .draw (ggplot .layout , ggplot .coordinates )
629
632
630
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.13" ):
633
+ if Version ( plotnine . __version__ ) >= Version ("0.13" ):
631
634
ggplot ._draw_panel_borders ()
632
635
ggplot .facet .theme = ggplot .theme
633
636
@@ -637,7 +640,7 @@ def draw_title(bricks, gori, gcp, figsize):
637
640
ggplot .theme .themeables ["figure_size" ] = new ("figure_size" ,(1 ,1 ))
638
641
ggplot .theme .apply ()
639
642
640
- elif StrictVersion ( plotnine_version ) >= StrictVersion ("0.9" ):
643
+ elif Version ( plotnine . __version__ ) >= Version ("0.9" ):
641
644
ggplot ._resize_panels ()
642
645
for i , l in enumerate (ggplot .layers , start = 1 ):
643
646
l .zorder = i + 10
@@ -646,7 +649,7 @@ def draw_title(bricks, gori, gcp, figsize):
646
649
ggplot ._draw_watermarks ()
647
650
ggplot .theme .apply (ggplot .figure , axs )
648
651
649
- elif StrictVersion ("0.8" ) <= StrictVersion ( plotnine_version ) < StrictVersion ("0.9" ):
652
+ elif Version ("0.8" ) <= Version ( plotnine . __version__ ) < Version ("0.9" ):
650
653
ggplot ._resize_panels ()
651
654
ggplot ._draw_layers ()
652
655
ggplot ._draw_breaks_and_labels ()
@@ -661,14 +664,13 @@ def draw_title(bricks, gori, gcp, figsize):
661
664
if "_ggplot_legend" in ax .__dict__ :
662
665
ax ._ggplot_legend = None #For Google colab...
663
666
ax .change_plotsize ((figsize [0 ], figsize [1 ]))
664
-
665
- if StrictVersion (plotnine_version ) >= StrictVersion ("0.9" ):
667
+ if Version (plotnine .__version__ ) >= Version ("0.9" ):
666
668
xl , yl = draw_labels (ax , ggplot , gcp , figsize )
667
669
668
670
draw_legend (ax , ggplot , gcp , figsize ) #0.13 makes Erros here.
669
671
draw_title (ax , ggplot , gcp , figsize )
670
-
671
- elif StrictVersion ("0.8" ) <= StrictVersion ( plotnine_version ) < StrictVersion ("0.9" ):
672
+
673
+ elif Version ("0.8" ) <= Version ( plotnine . __version__ ) < Version ("0.9" ):
672
674
draw_labels (ax , ggplot , gcp , figsize )
673
675
draw_legend (ax , ggplot , gcp , figsize )
674
676
draw_title (ax , ggplot , gcp , figsize )
@@ -682,7 +684,7 @@ def draw_title(bricks, gori, gcp, figsize):
682
684
axtmp = _axes_dict [key ]
683
685
axtmp .set_position (position_dict [key ])
684
686
685
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
687
+ if Version ( plotnine . __version__ ) >= Version ("0.12" ):
686
688
ax .set_xlabel (xl )
687
689
ax .set_ylabel (yl )
688
690
return_obj = ax
@@ -701,13 +703,13 @@ def draw_title(bricks, gori, gcp, figsize):
701
703
bricks = Bricks (bricks_dict = bricks_dict )
702
704
bricks = expand (bricks , width , height )
703
705
704
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.9" ):
706
+ if Version ( plotnine . __version__ ) >= Version ("0.9" ):
705
707
xl , yl = draw_labels (bricks , ggplot , gcp , figsize )
706
708
draw_legend (bricks , ggplot , gcp , figsize )
707
709
draw_title (bricks , ggplot , gcp , figsize )
708
710
pass
709
-
710
- elif StrictVersion ("0.8" ) <= StrictVersion ( plotnine_version ) < StrictVersion ("0.9" ):
711
+
712
+ elif Version ("0.8" ) <= Version ( plotnine . __version__ ) < Version ("0.9" ):
711
713
draw_labels (bricks , ggplot , gcp , figsize )
712
714
draw_legend (bricks , ggplot , gcp , figsize )
713
715
draw_title (bricks , ggplot , gcp , figsize )
@@ -724,12 +726,12 @@ def draw_title(bricks, gori, gcp, figsize):
724
726
x0 , x1 , y0 , y1 = bricks .get_outer_corner ()
725
727
bricks ._originalsize = (abs (x1 - x0 ), abs (y0 - y1 ))
726
728
bricks .set_originalpositions ()
727
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
729
+ if Version ( plotnine . __version__ ) >= Version ("0.12" ):
728
730
bricks .case .set_xlabel (xl )
729
731
bricks .case .set_ylabel (yl )
730
732
return_obj = bricks
731
733
732
- if StrictVersion ( plotnine_version ) >= StrictVersion ("0.12" ):
734
+ if Version ( plotnine . __version__ ) >= Version ("0.12" ):
733
735
matplotlib .rcParams ["figure.subplot.wspace" ] = figure_subplot_wspace_ori
734
736
matplotlib .rcParams ["figure.subplot.hspace" ] = figure_subplot_hspace_ori
735
737
return_obj .savefig (_ggplot = True )
@@ -2405,7 +2407,7 @@ def move_legend(self, new_loc, **kws):
2405
2407
"""
2406
2408
old_legend = self ._case .legend_
2407
2409
2408
- if (matplotlib .__version__ ) >= StrictVersion ("3.8" ):
2410
+ if Version (matplotlib .__version__ ) >= Version ("3.8" ):
2409
2411
handles = old_legend .legend_handles
2410
2412
else :
2411
2413
handles = old_legend .legendHandles
@@ -3080,7 +3082,7 @@ def move_legend(self, new_loc, **kws):
3080
3082
self ._comeback ()
3081
3083
old_legend = self .legend_
3082
3084
3083
- if (matplotlib .__version__ ) >= StrictVersion ("3.8" ):
3085
+ if Version (matplotlib .__version__ ) >= Version ("3.8" ):
3084
3086
handles = old_legend .legend_handles
3085
3087
else :
3086
3088
handles = old_legend .legendHandles
@@ -3598,7 +3600,7 @@ def resize(self, direction):
3598
3600
#bricks2 = (brick2 | (brick5 / brick4)) / (brick1 | brick3)
3599
3601
ax21543 = (ax2 / ax1 ) | (ax5 / ax4 / ax3 )
3600
3602
ax21543 .savefig ("test2.pdf" )
3601
-
3602
- if StrictVersion (plotnine .__version__ ) >= StrictVersion ("0.12" ):
3603
+
3604
+ if Version (plotnine .__version__ ) >= Version ("0.12" ):
3603
3605
overwrite_plotnine ()
3604
3606
0 commit comments