@@ -108,12 +108,12 @@ impl From<&ImportError> for Error {
108
108
/// Import and return a handle to the module `$m`.
109
109
macro_rules! pyimport { ( $name: path, $m: literal) => {
110
110
Python :: with_gil( |py|
111
- match PyModule :: import_bound ( py, intern!( py, $m) ) {
111
+ match PyModule :: import ( py, intern!( py, $m) ) {
112
112
Ok ( m) => Ok ( m. into( ) ) ,
113
113
Err ( e) => {
114
114
let mut msg = stringify!( $name) . to_string( ) ;
115
115
msg. push_str( ": " ) ;
116
- if let Ok ( s) = e. value_bound ( py) . str ( ) {
116
+ if let Ok ( s) = e. value ( py) . str ( ) {
117
117
let s = s. to_str( ) . unwrap_or( "Import error" ) ;
118
118
msg. push_str( s)
119
119
}
@@ -133,6 +133,13 @@ lazy_static! {
133
133
} ;
134
134
}
135
135
136
+ // RuntimeWarning: More than 20 figures have been opened. Figures
137
+ // created through the pyplot interface (`matplotlib.pyplot.figure`)
138
+ // are retained until explicitly closed and may consume too much
139
+ // memory. […] Consider using `matplotlib.pyplot.close()`.
140
+ //
141
+ // => Do not use pyplot interface (since we need handles anyway).
142
+
136
143
137
144
/// Container for most of the (sub-)plot elements: Axis, Tick,
138
145
/// [`Line2D`], Text, Polygon, etc., and sets the coordinate system.
@@ -246,9 +253,9 @@ impl Figure {
246
253
/// Default width: 6.4, default height: 4.8
247
254
pub fn set_size_inches ( & mut self , width : f64 , height : f64 ) -> & mut Self {
248
255
Python :: with_gil ( |py| {
249
- let kwargs = PyDict :: new_bound ( py) ;
256
+ let kwargs = PyDict :: new ( py) ;
250
257
kwargs. set_item ( "size_inches" , ( width, height) ) . unwrap ( ) ;
251
- self . fig . call_method_bound ( py, intern ! ( py, "set" ) , ( ) ,
258
+ self . fig . call_method ( py, intern ! ( py, "set" ) , ( ) ,
252
259
Some ( & kwargs) ) . unwrap ( ) ;
253
260
} ) ;
254
261
self
@@ -274,11 +281,11 @@ impl<'a> Savefig<'a> {
274
281
275
282
pub fn to_file ( & self , path : impl AsRef < Path > ) -> Result < ( ) , Error > {
276
283
Python :: with_gil ( |py| {
277
- let kwargs = PyDict :: new_bound ( py) ;
284
+ let kwargs = PyDict :: new ( py) ;
278
285
if let Some ( dpi) = self . dpi {
279
286
kwargs. set_item ( "dpi" , dpi) . unwrap ( )
280
287
}
281
- self . fig . call_method_bound (
288
+ self . fig . call_method (
282
289
py, intern ! ( py, "savefig" ) ,
283
290
( path. as_ref ( ) , ) , Some ( & kwargs)
284
291
) . map_err ( |e| {
@@ -509,8 +516,8 @@ impl Axes {
509
516
// FIXME: Do we want to check that `x` and `y` have the same
510
517
// dimension? Better error message?
511
518
meth ! ( self . ax, scatter, py -> {
512
- let xn = x. as_ref( ) . to_pyarray_bound ( py) ;
513
- let yn = y. as_ref( ) . to_pyarray_bound ( py) ;
519
+ let xn = x. as_ref( ) . to_pyarray ( py) ;
520
+ let yn = y. as_ref( ) . to_pyarray ( py) ;
514
521
( xn, yn) } )
515
522
. unwrap ( ) ;
516
523
self
@@ -588,13 +595,13 @@ impl Axes {
588
595
Python :: with_gil ( |py| {
589
596
let elements = lines. into_iter ( ) . map ( |l| l. line2d ) ;
590
597
if elements. len ( ) == 0 { // FIXME: .is_empty is unstable
591
- self . ax . call_method_bound ( py, intern ! ( py, "legend" ) , ( ) , None )
598
+ self . ax . call_method ( py, intern ! ( py, "legend" ) , ( ) , None )
592
599
. unwrap ( ) ;
593
600
} else {
594
- let dic = PyDict :: new_bound ( py) ;
595
- dic. set_item ( "handles" , PyList :: new_bound ( py, elements) )
601
+ let dic = PyDict :: new ( py) ;
602
+ dic. set_item ( "handles" , PyList :: new ( py, elements) . unwrap ( ) )
596
603
. unwrap ( ) ;
597
- self . ax . call_method_bound ( py, intern ! ( py, "legend" ) , ( ) ,
604
+ self . ax . call_method ( py, intern ! ( py, "legend" ) , ( ) ,
598
605
Some ( & dic) )
599
606
. unwrap ( ) ;
600
607
}
@@ -634,7 +641,7 @@ impl<'a> PlotOptions<'a> {
634
641
}
635
642
636
643
fn kwargs ( & ' a self , py : Python < ' a > ) -> Bound < ' a , PyDict > {
637
- let kwargs = PyDict :: new_bound ( py) ;
644
+ let kwargs = PyDict :: new ( py) ;
638
645
if self . animated {
639
646
kwargs. set_item ( "animated" , true ) . unwrap ( )
640
647
}
@@ -650,7 +657,7 @@ impl<'a> PlotOptions<'a> {
650
657
kwargs. set_item ( "markersize" , w) . unwrap ( )
651
658
}
652
659
if let Some ( rgba) = self . color {
653
- let color = PyTuple :: new_bound ( py, rgba) ;
660
+ let color = PyTuple :: new ( py, rgba) . unwrap ( ) ;
654
661
kwargs. set_item ( "color" , color) . unwrap ( )
655
662
}
656
663
kwargs
@@ -660,9 +667,9 @@ impl<'a> PlotOptions<'a> {
660
667
fn plot_xy (
661
668
& self , py : Python < ' _ > , axes : & Axes , x : & [ f64 ] , y : & [ f64 ]
662
669
) -> Line2D {
663
- let x = x. to_pyarray_bound ( py) ;
664
- let y = y. to_pyarray_bound ( py) ;
665
- let lines = axes. ax . call_method_bound ( py,
670
+ let x = x. to_pyarray ( py) ;
671
+ let y = y. to_pyarray ( py) ;
672
+ let lines = axes. ax . call_method ( py,
666
673
"plot" , ( x, y, self . fmt ) , Some ( & self . kwargs ( py) ) ) . unwrap ( ) ;
667
674
let lines: & Bound < PyList > = lines. downcast_bound ( py) . unwrap ( ) ;
668
675
// Extract the element from the list of length 1 (1 data plotted)
@@ -672,8 +679,8 @@ impl<'a> PlotOptions<'a> {
672
679
673
680
fn plot_y ( & self , py : Python < ' _ > , axes : & Axes , y : & [ f64 ] ) -> Line2D
674
681
{
675
- let y = y. to_pyarray_bound ( py) ;
676
- let lines = axes. ax . call_method_bound ( py,
682
+ let y = y. to_pyarray ( py) ;
683
+ let lines = axes. ax . call_method ( py,
677
684
"plot" , ( y, self . fmt ) , Some ( & self . kwargs ( py) ) ) . unwrap ( ) ;
678
685
let lines: & Bound < PyList > = lines. downcast_bound ( py) . unwrap ( ) ;
679
686
let line2d = lines. get_item ( 0 ) . unwrap ( ) . into ( ) ;
@@ -902,26 +909,26 @@ macro_rules! set_contour_options { () => {
902
909
let py = d. py( ) ;
903
910
if let Some ( levels) = self . levels {
904
911
let n = levels. len( ) ;
905
- let levels = levels. to_pyarray_bound ( py) ;
912
+ let levels = levels. to_pyarray ( py) ;
906
913
d. set_item( "levels" , levels) . unwrap( ) ;
907
914
908
915
if let Some ( colors) = & self . colors {
909
916
if colors. len( ) >= n {
910
- let colors = PyList :: new_bound ( py, colors) ;
917
+ let colors = PyList :: new ( py, colors) . unwrap ( ) ;
911
918
d. set_item( "colors" , colors) . unwrap( ) ;
912
919
} else {
913
920
let default = self . options. color. unwrap_or( [ 0. , 0. , 0. , 1. ] ) ;
914
921
let mut colors = colors. clone( ) ;
915
922
for _ in 0 .. n - colors. len( ) {
916
923
colors. push( default ) ;
917
924
}
918
- let colors = PyList :: new_bound ( py, colors) ;
925
+ let colors = PyList :: new ( py, colors) . unwrap ( ) ;
919
926
d. set_item( "colors" , colors) . unwrap( ) ;
920
927
}
921
928
} else if let Some ( color) = self . options. color {
922
929
// let colors = std::iter::repeat_n(color, n);
923
930
let colors = vec![ color; n] ;
924
- let colors = PyList :: new_bound ( py, colors) ;
931
+ let colors = PyList :: new ( py, colors) . unwrap ( ) ;
925
932
d. set_item( "colors" , colors) . unwrap( ) ;
926
933
}
927
934
}
@@ -946,13 +953,13 @@ where D: AsRef<[f64]> {
946
953
947
954
pub fn plot ( & self ) -> QuadContourSet {
948
955
Python :: with_gil ( |py| {
949
- let x = self . x . as_ref ( ) . to_pyarray_bound ( py) ;
950
- let y = self . y . as_ref ( ) . to_pyarray_bound ( py) ;
951
- let z = self . z . to_pyarray_bound ( py) ;
956
+ let x = self . x . as_ref ( ) . to_pyarray ( py) ;
957
+ let y = self . y . as_ref ( ) . to_pyarray ( py) ;
958
+ let z = self . z . to_pyarray ( py) ;
952
959
let mut opt = self . options . kwargs ( py) ;
953
960
self . update_dict ( & mut opt) ;
954
961
let contours = self . axes . ax
955
- . call_method_bound ( py, intern ! ( py, "contour" ) ,
962
+ . call_method ( py, intern ! ( py, "contour" ) ,
956
963
( x, y, z) ,
957
964
Some ( & opt) )
958
965
. unwrap ( ) ;
@@ -1000,13 +1007,13 @@ where F: FnMut(f64, f64) -> f64 {
1000
1007
}
1001
1008
}
1002
1009
Python :: with_gil ( |py| {
1003
- let x = x. to_pyarray_bound ( py) ;
1004
- let y = y. to_pyarray_bound ( py) ;
1005
- let z = z. to_pyarray_bound ( py) ;
1010
+ let x = x. to_pyarray ( py) ;
1011
+ let y = y. to_pyarray ( py) ;
1012
+ let z = z. to_pyarray ( py) ;
1006
1013
let mut opt = self . options . kwargs ( py) ;
1007
1014
self . update_dict ( & mut opt) ;
1008
1015
let contours = self . axes . ax
1009
- . call_method_bound ( py, intern ! ( py, "contour" ) ,
1016
+ . call_method ( py, intern ! ( py, "contour" ) ,
1010
1017
( x, y, z) ,
1011
1018
Some ( & opt) )
1012
1019
. unwrap ( ) ;
@@ -1017,18 +1024,22 @@ where F: FnMut(f64, f64) -> f64 {
1017
1024
1018
1025
1019
1026
impl Line2D {
1020
- fn set_kw ( & self , prop : & str , v : impl ToPyObject ) {
1021
- Python :: with_gil ( |py| {
1022
- let kwargs = PyDict :: new_bound ( py) ;
1023
- kwargs. set_item ( prop, v) . unwrap ( ) ;
1024
- self . line2d . call_method_bound ( py, "set" , ( ) , Some ( & kwargs) )
1025
- . unwrap ( ) ;
1026
- } )
1027
+ fn set_kw < ' a > (
1028
+ & self ,
1029
+ py : Python < ' a > ,
1030
+ prop : & str ,
1031
+ v : impl IntoPyObject < ' a >
1032
+ ) {
1033
+ let kwargs = PyDict :: new ( py) ;
1034
+ kwargs. set_item ( prop, v) . unwrap ( ) ;
1035
+ self . line2d . call_method ( py, "set" , ( ) , Some ( & kwargs) ) . unwrap ( ) ;
1027
1036
}
1028
1037
1029
1038
pub fn set_label ( & mut self , label : impl AsRef < str > ) -> & mut Self {
1030
- self . set_kw ( "label" , label. as_ref ( ) ) ;
1031
- self
1039
+ Python :: with_gil ( |py| {
1040
+ self . set_kw ( py, "label" , label. as_ref ( ) ) ;
1041
+ self
1042
+ } )
1032
1043
}
1033
1044
1034
1045
/// Set the color of the line to `c`.
@@ -1040,13 +1051,17 @@ impl Line2D {
1040
1051
}
1041
1052
1042
1053
pub fn set_linewidth ( & mut self , w : f64 ) -> & mut Self {
1043
- self . set_kw ( "linewidth" , w) ;
1044
- self
1054
+ Python :: with_gil ( |py| {
1055
+ self . set_kw ( py, "linewidth" , w) ;
1056
+ self
1057
+ } )
1045
1058
}
1046
1059
1047
1060
pub fn linewidth ( self , w : f64 ) -> Self {
1048
- self . set_kw ( "linewidth" , w) ;
1049
- self
1061
+ Python :: with_gil ( |py| {
1062
+ self . set_kw ( py, "linewidth" , w) ;
1063
+ self
1064
+ } )
1050
1065
}
1051
1066
}
1052
1067
0 commit comments