@@ -22,26 +22,14 @@ use crate::{axes::Axes, Error, figure::Figure, IntoError};
2222//
2323// => Do not use pyplot interface (since we need handles anyway).
2424
25- fn pyplot < ' a , ' py > (
26- py : Python < ' py > ,
27- f : & ' a PyOnceLock < Py < PyAny > > ,
28- attr_name : & str
29- ) -> Result < & ' a Bound < ' py , PyAny > , Error > {
30- f. get_or_try_init ( py, || {
31- Ok ( py. import ( "matplotlib.pyplot" ) . into_error ( py) ?
32- . getattr ( attr_name) . into_error ( py) ?
33- . unbind ( ) )
34- } )
35- . map ( |f| f. bind ( py) )
36- }
37-
3825/// Return a new figure.
3926/// This figure is tracked by Matplotlib so [`show()`] displays it.
4027/// This implies it must be explicitly deallocated using [`close()`].
4128pub fn figure ( ) -> Result < Figure , Error > {
4229 static FIG : PyOnceLock < Py < PyAny > > = PyOnceLock :: new ( ) ;
4330 Python :: attach ( |py| {
44- let fig = pyplot ( py, & FIG , "figure" ) ?;
31+ let fig = FIG . import ( py, "matplotlib.pyplot" , "figure" )
32+ . into_error ( py) ?;
4533 Ok ( Figure { fig : fig. call0 ( ) . into_error ( py) ?. unbind ( ) } )
4634 } )
4735}
@@ -58,7 +46,7 @@ pub fn subplots<const R: usize, const C: usize>(
5846pub fn show ( ) {
5947 static SHOW : PyOnceLock < Py < PyAny > > = PyOnceLock :: new ( ) ;
6048 Python :: attach ( |py| {
61- pyplot ( py, & SHOW , "show" )
49+ SHOW . import ( py, "matplotlib.pyplot" , "show" )
6250 . expect ( "Cannot get matplotlib.pyplot.show" )
6351 . call0 ( )
6452 . expect ( "matplotlib.pyplot.show did not succeed" ) ;
@@ -69,7 +57,7 @@ pub fn show() {
6957pub fn close ( fig : Figure ) {
7058 static CLOSE : PyOnceLock < Py < PyAny > > = PyOnceLock :: new ( ) ;
7159 Python :: attach ( |py| {
72- pyplot ( py, & CLOSE , "close" )
60+ CLOSE . import ( py, "matplotlib.pyplot" , "close" )
7361 . expect ( "Cannot find matplotlib.pyplot.close" )
7462 . call1 ( ( fig. fig , ) )
7563 . expect ( "matplotlib.pyplot.close did not succeed" ) ;
@@ -80,7 +68,7 @@ pub fn close(fig: Figure) {
8068pub fn close_all ( ) {
8169 static CLOSE : PyOnceLock < Py < PyAny > > = PyOnceLock :: new ( ) ;
8270 Python :: attach ( |py| {
83- pyplot ( py, & CLOSE , "close" )
71+ CLOSE . import ( py, "matplotlib.pyplot" , "close" )
8472 . expect ( "Cannot get matplotlib.pyplot.close" )
8573 . call1 ( ( "all" , ) )
8674 . expect ( "matplotlib.pyplot.close('all') did not succeed" ) ;
0 commit comments