Coverage for src/plotly_gtk/demo.py: 0%

112 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-08 21:22 +0000

1# pylint: disable=all 

2import gi 

3import numpy as np 

4import plotly.express as px 

5import plotly.graph_objects as go 

6from plotly.subplots import make_subplots 

7 

8from plotly_gtk.chart import PlotlyGtk 

9from plotly_gtk.webview import FigureWebView 

10 

11gi.require_version("Adw", "1") 

12gi.require_version("Gtk", "4.0") 

13from gi.repository import Adw, Gtk 

14 

15scatter_demos = [ 

16 "scatter_1", 

17 "scatter_2", 

18 "scatter_size_color_column", 

19 "scatter_facetting", 

20 "line_1", 

21 "line_2", 

22] 

23log_demos = ["log_1", "log_2"] 

24multiple_axes_demos = [ 

25 "two_y_axes", 

26 "multiple_y_axes_subplots", 

27 "multiple_axes", 

28 "autoshift", 

29 "shift_by_pixels", 

30 # "syncticks", 

31] 

32 

33demos = { 

34 "Scatter": scatter_demos, 

35 "log": log_demos, 

36 "Multiple axes": multiple_axes_demos, 

37} 

38 

39 

40def get_test_figure(reference): 

41 if reference in scatter_demos: 

42 return _get_scatter_test_figure(reference) 

43 if reference in log_demos: 

44 return _get_log_test_figure(reference) 

45 if reference in multiple_axes_demos: 

46 return _get_multiple_axes_test_figure(reference) 

47 

48 

49def _get_log_test_figure(reference): 

50 if reference == "log_1": 

51 df = px.data.gapminder().query("year == 2007") 

52 fig = px.scatter( 

53 df, x="gdpPercap", y="lifeExp", hover_name="country", log_x=True 

54 ) 

55 elif reference == "log_2": 

56 df = px.data.gapminder().query("year == 2007") 

57 fig = px.scatter( 

58 df, 

59 x="gdpPercap", 

60 y="lifeExp", 

61 hover_name="country", 

62 log_x=True, 

63 range_x=[1, 100000], 

64 range_y=[0, 100], 

65 ) 

66 return fig 

67 

68 

69def _get_scatter_test_figure(reference): 

70 if reference == "scatter_1": 

71 fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16]) 

72 elif reference == "scatter_2": 

73 df = px.data.iris() 

74 fig = px.scatter(df, x="sepal_width", y="sepal_length") 

75 elif reference == "scatter_size_color_column": 

76 df = px.data.iris() 

77 fig = px.scatter( 

78 df, 

79 x="sepal_width", 

80 y="sepal_length", 

81 color="species", 

82 size="petal_length", 

83 hover_data=["petal_width"], 

84 ) 

85 elif reference == "scatter_facetting": 

86 df = px.data.tips() 

87 fig = px.scatter( 

88 df, 

89 x="total_bill", 

90 y="tip", 

91 color="smoker", 

92 facet_col="sex", 

93 facet_row="time", 

94 ) 

95 elif reference == "line_1": 

96 t = np.linspace(0, 2 * np.pi, 100) 

97 fig = px.line(x=t, y=np.cos(t), labels={"x": "t", "y": "cos(t)"}) 

98 elif reference == "line_2": 

99 df = px.data.gapminder().query("continent == 'Oceania'") 

100 fig = px.line(df, x="year", y="lifeExp", color="country") 

101 return fig 

102 

103 

104def _get_multiple_axes_test_figure(reference): 

105 if reference == "two_y_axes": 

106 # Create figure with secondary y-axis 

107 fig = make_subplots(specs=[[{"secondary_y": True}]]) 

108 

109 # Add traces 

110 fig.add_trace( 

111 go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"), 

112 secondary_y=False, 

113 ) 

114 

115 fig.add_trace( 

116 go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data"), 

117 secondary_y=True, 

118 ) 

119 

120 # Add figure title 

121 fig.update_layout(title_text="Double Y Axis Example") 

122 

123 # Set x-axis title 

124 fig.update_xaxes(title_text="xaxis title") 

125 

126 # Set y-axes titles 

127 fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False) 

128 fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True) 

129 elif reference == "multiple_y_axes_subplots": 

130 fig = make_subplots( 

131 rows=2, 

132 cols=2, 

133 specs=[ 

134 [{"secondary_y": True}, {"secondary_y": True}], 

135 [{"secondary_y": True}, {"secondary_y": True}], 

136 ], 

137 ) 

138 

139 # Top left 

140 fig.add_trace( 

141 go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis data"), 

142 row=1, 

143 col=1, 

144 secondary_y=False, 

145 ) 

146 

147 fig.add_trace( 

148 go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis2 data"), 

149 row=1, 

150 col=1, 

151 secondary_y=True, 

152 ) 

153 

154 # Top right 

155 fig.add_trace( 

156 go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis3 data"), 

157 row=1, 

158 col=2, 

159 secondary_y=False, 

160 ) 

161 

162 fig.add_trace( 

163 go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis4 data"), 

164 row=1, 

165 col=2, 

166 secondary_y=True, 

167 ) 

168 

169 # Bottom left 

170 fig.add_trace( 

171 go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis5 data"), 

172 row=2, 

173 col=1, 

174 secondary_y=False, 

175 ) 

176 

177 fig.add_trace( 

178 go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis6 data"), 

179 row=2, 

180 col=1, 

181 secondary_y=True, 

182 ) 

183 

184 # Bottom right 

185 fig.add_trace( 

186 go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis7 data"), 

187 row=2, 

188 col=2, 

189 secondary_y=False, 

190 ) 

191 

192 fig.add_trace( 

193 go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis8 data"), 

194 row=2, 

195 col=2, 

196 secondary_y=True, 

197 ) 

198 elif reference == "multiple_axes": 

199 fig = go.Figure() 

200 

201 fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis1 data")) 

202 

203 fig.add_trace( 

204 go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2") 

205 ) 

206 

207 fig.add_trace( 

208 go.Scatter( 

209 x=[4, 5, 6], y=[40000, 50000, 60000], name="yaxis3 data", yaxis="y3" 

210 ) 

211 ) 

212 

213 fig.add_trace( 

214 go.Scatter( 

215 x=[5, 6, 7], y=[400000, 500000, 600000], name="yaxis4 data", yaxis="y4" 

216 ) 

217 ) 

218 

219 # Create axis objects 

220 fig.update_layout( 

221 xaxis=dict(domain=[0.3, 0.7]), 

222 yaxis=dict( 

223 title=dict(text="yaxis title", font=dict(color="#1f77b4")), 

224 tickfont=dict(color="#1f77b4"), 

225 ), 

226 yaxis2=dict( 

227 title=dict(text="yaxis2 title", font=dict(color="#ff7f0e")), 

228 tickfont=dict(color="#ff7f0e"), 

229 anchor="free", 

230 overlaying="y", 

231 side="left", 

232 position=0.15, 

233 ), 

234 yaxis3=dict( 

235 title=dict(text="yaxis3 title", font=dict(color="#d62728")), 

236 tickfont=dict(color="#d62728"), 

237 anchor="x", 

238 overlaying="y", 

239 side="right", 

240 ), 

241 yaxis4=dict( 

242 title=dict(text="yaxis4 title", font=dict(color="#9467bd")), 

243 tickfont=dict(color="#9467bd"), 

244 anchor="free", 

245 overlaying="y", 

246 side="right", 

247 position=0.85, 

248 ), 

249 ) 

250 

251 # Update layout properties 

252 fig.update_layout( 

253 title_text="multiple y-axes example", 

254 width=800, 

255 ) 

256 elif reference == "autoshift": 

257 fig = go.Figure() 

258 

259 fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis data")) 

260 

261 fig.add_trace( 

262 go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2") 

263 ) 

264 

265 fig.add_trace( 

266 go.Scatter( 

267 x=[4, 5, 6], y=[1000, 2000, 3000], name="yaxis3 data", yaxis="y3" 

268 ) 

269 ) 

270 

271 fig.add_trace( 

272 go.Scatter(x=[3, 4, 5], y=[400, 500, 600], name="yaxis4 data", yaxis="y4") 

273 ) 

274 

275 fig.update_layout( 

276 xaxis=dict(domain=[0.25, 0.75]), 

277 yaxis=dict( 

278 title="yaxis title", 

279 ), 

280 yaxis2=dict( 

281 title="yaxis2 title", 

282 overlaying="y", 

283 side="right", 

284 ), 

285 yaxis3=dict( 

286 title="yaxis3 title", anchor="free", overlaying="y", autoshift=True 

287 ), 

288 yaxis4=dict( 

289 title="yaxis4 title", 

290 anchor="free", 

291 overlaying="y", 

292 autoshift=True, 

293 ), 

294 ) 

295 

296 fig.update_layout( 

297 title_text="Shifting y-axes with autoshift", 

298 ) 

299 elif reference == "shift_by_pixels": 

300 fig = go.Figure() 

301 

302 fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis data")) 

303 

304 fig.add_trace( 

305 go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2") 

306 ) 

307 

308 fig.add_trace( 

309 go.Scatter( 

310 x=[4, 5, 6], y=[1000, 2000, 3000], name="yaxis3 data", yaxis="y3" 

311 ) 

312 ) 

313 

314 fig.add_trace( 

315 go.Scatter(x=[3, 4, 5], y=[400, 500, 600], name="yaxis4 data", yaxis="y4") 

316 ) 

317 

318 fig.update_layout( 

319 xaxis=dict(domain=[0.25, 0.75]), 

320 yaxis=dict( 

321 title="yaxis title", 

322 ), 

323 yaxis2=dict( 

324 title="yaxis2 title", 

325 overlaying="y", 

326 side="right", 

327 ), 

328 yaxis3=dict( 

329 title="yaxis3 title", anchor="free", overlaying="y", autoshift=True 

330 ), 

331 yaxis4=dict( 

332 title="yaxis4 title", 

333 anchor="free", 

334 overlaying="y", 

335 autoshift=True, 

336 shift=-100, 

337 ), 

338 ) 

339 

340 fig.update_layout( 

341 title_text="Shifting y-axes by a specific number of pixels", 

342 ) 

343 else: 

344 return 

345 return fig 

346 

347 

348def test(app): 

349 window = Adw.Window(title="Plotly GTK test", application=app) 

350 paned = Gtk.Paned() 

351 window.set_content(paned) 

352 

353 fig = get_test_figure("multiple_y_axes_subplots") 

354 print(fig) 

355 # print(fig["layout"]["template"]) 

356 

357 webview = FigureWebView(fig) 

358 webview.set_hexpand(True) 

359 webview.set_vexpand(True) 

360 

361 paned.set_start_child(webview) 

362 paned.set_end_child(PlotlyGtk(fig)) 

363 window.present() 

364 webview.refresh() 

365 

366 

367def standalone(): 

368 app = Adw.Application(application_id="io.github.slaclau.plotly_gtk.test") 

369 app.connect("activate", test) 

370 app.run() 

371 

372 

373if __name__ == "__main__": 

374 standalone()