66
77import matplotlib .pyplot as plt
88import numpy as np
9+ import seaborn as sns
910from PIL import Image
10- from plotnine import aes , element_rect , facet_wrap , geom_point , stat_smooth , theme
11+ from plotnine import aes , element_rect , geom_point , theme , theme_minimal
1112from plotnine .data import mtcars
1213from plotnine .ggplot import ggplot
1314
1415from shiny import App , Inputs , Outputs , Session , module , render , req , ui
1516
17+ tips = sns .load_dataset ("tips" )
18+ dpi = 150
19+
1620
1721@module .ui
1822def plot_ui ():
@@ -51,7 +55,7 @@ def plot_dom_size():
5155 def plot_decorator_size ():
5256 return plot_fn (None )
5357
54- @render .plot
58+ @render .plot ( width = 0 , height = 0 )
5559 def plot_native_size ():
5660 return plot_fn ((300 , 200 ))
5761
@@ -65,12 +69,19 @@ def plot_native_size():
6569 plot_ui ("mpl" ),
6670 value = "mpl" ,
6771 ),
72+ ui .nav (
73+ "seaborn" ,
74+ ui .p (
75+ "The following four plots should all be the same size. The last one should have larger text."
76+ ),
77+ plot_ui ("sns" ),
78+ value = "sns" ,
79+ ),
6880 ui .nav (
6981 "plotnine" ,
7082 ui .p (
7183 "The following four plots should all be the same size. The last one should have larger text."
7284 ),
73- ui .p ("It may take a moment for the plots to render." ),
7485 plot_ui ("plotnine" ),
7586 ),
7687 ui .nav (
@@ -99,19 +110,31 @@ def plot_with_mpl(fig_size: tuple[float, float] | None) -> object:
99110
100111 return fig
101112
113+ def plot_with_sns (fig_size : tuple [float , float ] | None ) -> object :
114+ kwargs = dict ()
115+ if fig_size :
116+ kwargs ["height" ] = fig_size [1 ] / dpi
117+ kwargs ["aspect" ] = fig_size [0 ] / fig_size [1 ]
118+
119+ # FacetGrid has an opinion about its figure size
120+ g = sns .FacetGrid (tips , ** kwargs ) # pyright: ignore[reportUnknownArgumentType]
121+ g .figure .set_facecolor ("lavender" )
122+ g .map (sns .scatterplot , "total_bill" , "tip" )
123+ plt .gca ().set_facecolor ("lavender" )
124+ if fig_size :
125+ plt .gcf ().set_dpi (dpi )
126+
102127 def plot_with_plotnine (fig_size : tuple [float , float ] | None ) -> object :
103128 p = (
104- ggplot (mtcars , aes ("wt" , "mpg" , color = "factor(gear)" ))
129+ ggplot (mtcars , aes ("wt" , "mpg" ))
105130 + geom_point ()
106- + stat_smooth (method = "lm" )
107- + facet_wrap ("~gear" )
131+ + theme_minimal ()
108132 + theme (
109133 plot_background = element_rect (fill = "lavender" ),
110134 legend_background = element_rect (fill = "lavender" ),
111135 )
112136 )
113137 if fig_size is not None :
114- dpi = 150
115138 p = p + theme (
116139 figure_size = (fig_size [0 ] / dpi , fig_size [1 ] / dpi ),
117140 plot_background = element_rect (fill = "lavender" ),
@@ -124,6 +147,7 @@ def plot_with_pil(fig_size: tuple[float, float] | None) -> object:
124147 return Image .open (Path (__file__ ).parent / "bike.jpg" )
125148
126149 plot_server ("mpl" , plot_with_mpl )
150+ plot_server ("sns" , plot_with_sns )
127151 plot_server ("plotnine" , plot_with_plotnine )
128152 plot_server ("pil" , plot_with_pil )
129153
0 commit comments