Re: Toggling dark/light GUI mode / changing matplotlib style
Matt Newville <[email protected]> Mon, 7 Mar 2022 10:50:14 -0600
| Newsgroups | gmane.comp.python.wxpython |
|---|---|
| Message-ID | <CA+7ESboq5Eq74NikcXu_OxJ1r=uWXuFs4XBkUX+FEZSzkXLxMg@mail.gmail.com> |
--000000000000b59d5505d9a3aad3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 7, 2022 at 10:07 AM Matthias Brennwald <[email protected]> wrote: > > On Sa, M=C3=A4r 5 2022 at 06:18:51 -0600, Matt Newville < > [email protected]> wrote: > > > > On Sat, Mar 5, 2022 at 2:07 AM Matthias Brennwald <[email protected]> > wrote: > >> >> >> On Fri, Mar 4 2022 at 22:24:17 -0600, Matt Newville < >> [email protected]> wrote: >> >> ... One of the things it supports is setting one of matplolib's color >> themes (including many from seaborn). This essentially sets the matplot= lib >> rcParams settings and sets the colors of the various plot components. >> >> >> Sure. That's exactly what I am already doing now (use rcParams to set th= e >> plot style). However, according to the matplotlib documentation, changin= g >> the plot styles (via rcParams) of an existing plot does not work. Settin= g >> the plot styles using rcParams will therefore only work at startup, when >> the figure is created, but it does not allow changing the plot style if = the >> GUI light/dark mode is changed *after* the figure has been created. >> > > Sorry to repeat myself but: > a) providing example code would clarify what you are doing. > b) wxmplot might do what you want. > c) even if not, the wxmplot code will show you an example of one way > this can be done. > d) wxmplot sets rcParams **and** sets the colors of the plot component= s. > > See: > https://github.com/newville/wxmplot/blob/master/wxmplot/config.py#L313 > > > Okay, here's a simple code example using wxmplot as a starting point: > > ---------------------------------------------- > import wx > import wxmplot > import numpy > > x =3D numpy.arange(100)/20.0 + numpy.random.random(size=3D100) > y =3D numpy.random.random(size=3Dlen(x)) > app =3D wx.App() > pframe =3D wxmplot.PlotFrame() > pframe.plot(x, y, color=3D'k', xlabel=3D'x axis', ylabel=3D'y axis') > pframe.Show() > app.MainLoop() > ---------------------------------------------- > > If I run this and then change the GUI from "light" to "dark" mode (and > back), all the control elements follow the change, but not the plot itsel= f > (see attached screen capture). To change the plot style ("light" / "dark"= ), > I'd need to be able to capture the paint event for the frame, and then > somehow call the PlotConfig.set_theme(...) from the link Matts' message. > However, I don't see how I can hook into the event loop / event handler o= f > the wxmplot panel. Can someone help me out? > Hmm, that (setting the theme from the configuration frame) works (updating all colors and styling) for me on Windows 10, MacOS, and Linux (CentOS 7), all using Python 3.8 or 3.9, and wxPython 4.1 or later. To be clear, you should be able to use pframe.plot(x, y, color=3D'k', xlabel=3D'x axis', ylabel=3D'y axis', theme=3D'dark') but there is some ambiguity in setting the theme and trace color -- the default is to obey the explicitly set colors, so that would draw a black trace on a dark background. If you want to set the theme programmatically, it would be "PlotPanel.conf.set_theme('seaborn-bright')" or "PlotFrame.panel.conf.set_theme('matplotlib')". And, as a general rule you can force a redraw with "PlotPanel.canvas.draw()". --Matt --=20 You received this message because you are subscribed to the Google Groups "= wxPython-users" group. To unsubscribe from this group and stop receiving emails from it, send an e= mail to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/= wxpython-users/CA%2B7ESboq5Eq74NikcXu_OxJ1r%3DuWXuFs4XBkUX%2BFEZSzkXLxMg%40= mail.gmail.com. --000000000000b59d5505d9a3aad3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"fon= t-family:arial,sans-serif"><br></div></div><br><div class=3D"gmail_quote"><= div dir=3D"ltr" class=3D"gmail_attr">On Mon, Mar 7, 2022 at 10:07 AM Matthi= as Brennwald <<a href=3D"mailto:[email protected]">[email protected]</= a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0p= x 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><d= iv id=3D"gmail-m_-7980941990418181303geary-body" dir=3D"auto"><br></div><di= v id=3D"gmail-m_-7980941990418181303geary-quote" dir=3D"auto">On Sa, M=C3= =A4r 5 2022 at 06:18:51 -0600, Matt Newville <<a href=3D"mailto:newville= @cars.uchicago.edu" target=3D"_blank">[email protected]</a>> wr= ote:<br><blockquote type=3D"cite"><div dir=3D"ltr"><div dir=3D"ltr"><div st= yle=3D"font-family:arial,sans-serif"><br></div></div><br><div class=3D"gmai= l_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, Mar 5, 2022 at 2:07 = AM Matthias Brennwald <<a href=3D"mailto:[email protected]" target=3D"_= blank">[email protected]</a>> wrote:<br></div><blockquote class=3D"gmai= l_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,20= 4,204);padding-left:1ex"><div id=3D"gmail-m_-7980941990418181303gmail-m_631= 8225154164708065geary-body" dir=3D"auto"><div><br></div></div><div id=3D"gm= ail-m_-7980941990418181303gmail-m_6318225154164708065geary-quote" dir=3D"au= to"><br>On Fri, Mar 4 2022 at 22:24:17 -0600, Matt Newville <<a href=3D"= mailto:[email protected]" target=3D"_blank">[email protected]= .edu</a>> wrote:<br><blockquote type=3D"cite"><div dir=3D"ltr"><div dir= =3D"ltr"><div style=3D"font-family:arial,sans-serif">... One of the things = it supports is setting one of matplolib's=C2=A0color themes (including = many from seaborn).=C2=A0 This essentially sets the matplotlib rcParams set= tings and sets the colors of the various=C2=A0plot components.</div></div><= /div></blockquote><font face=3D"arial, sans-serif"><div><font face=3D"arial= , sans-serif"><br></font></div><div><font face=3D"arial, sans-serif">Sure. = That's exactly what I am already doing now (use rcParams to set the plo= t style). However, according to the matplotlib documentation, changing the = plot styles (via rcParams) of an existing plot does not work. Setting the p= lot styles using rcParams will therefore only work at startup, when the fig= ure is created, but it does not allow changing the plot style if the GUI li= ght/dark mode is changed *after* the figure has been created.</font></div><= /font></div></blockquote><div><br></div><div style=3D"font-family:arial,san= s-serif">Sorry to repeat myself but:=C2=A0</div><div style=3D"font-family:a= rial,sans-serif">=C2=A0 =C2=A0a) providing example code would clarify what = you are doing.=C2=A0</div><div style=3D"font-family:arial,sans-serif">=C2= =A0 =C2=A0b) wxmplot might do what you want.</div><div style=3D"font-family= :arial,sans-serif">=C2=A0 =C2=A0c) even if not, the wxmplot code will show = you an example of one way this can be done.=C2=A0</div><div style=3D"font-f= amily:arial,sans-serif">=C2=A0 =C2=A0d) wxmplot sets rcParams **and** sets = the colors of the plot components.</div><div style=3D"font-family:arial,san= s-serif"><br></div><div style=3D"font-family:arial,sans-serif">See: <a href= =3D"https://github.com/newville/wxmplot/blob/master/wxmplot/config.py#L313"= target=3D"_blank">https://github.com/newville/wxmplot/blob/master/wxmplot/= config.py#L313</a></div></div></div></blockquote><font face=3D"arial, sans-= serif"><div><font face=3D"arial, sans-serif"><br></font></div><div>Okay, he= re's a simple code example using wxmplot as a starting point:</div></fo= nt><font face=3D"arial, sans-serif"><div><br></div><div><div>--------------= --------------------------------</div></div><div><div>import wx</div><div>i= mport wxmplot</div><div>import numpy</div><div><br></div><div>x =C2=A0 =3D = numpy.arange(100)/20.0 + numpy.random.random(size=3D100)</div><div>y =C2=A0= =3D numpy.random.random(size=3Dlen(x))</div><div>app =3D wx.App()</div><di= v>pframe =3D wxmplot.PlotFrame() =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0=C2=A0</div><div><span class=3D"gmail_default" style=3D"font-family:a= rial,sans-serif"></span>pframe.plot(x, y, color=3D'k', xlabel=3D= 9;x axis', ylabel=3D'y axis')</div><div>pframe.Show()</div><div= >app.MainLoop()</div></div><div>-------------------------------------------= ---</div><div><br></div><div>If I run this and then change the GUI from &qu= ot;light" to "dark" mode (and back), all the control element= s follow the change, but not the plot itself (see attached screen capture).= To change the plot style ("light" / "dark"), I'd n= eed to be able to capture the paint event for the frame, and then somehow c= all the PlotConfig.set_theme(...) from the link Matts' message. However= , I don't see how I can hook into the event loop / event handler of the= wxmplot panel. Can someone help me out?</div></font></div></blockquote><di= v><br></div><div class=3D"gmail_default" style=3D"font-family:arial,sans-se= rif">Hmm, that (setting the theme from the configuration frame) works (upda= ting all colors and styling) for me on Windows 10, MacOS, and Linux (CentOS= 7), all using Python 3.8 or 3.9, and wxPython 4.1 or later.=C2=A0=C2=A0</d= iv><div class=3D"gmail_default" style=3D"font-family:arial,sans-serif"><br>= </div><div class=3D"gmail_default" style=3D"font-family:arial,sans-serif">T= o be clear, you should be able to use</div><div class=3D"gmail_default" sty= le=3D"font-family:arial,sans-serif"><span class=3D"gmail_default">=C2=A0 = =C2=A0</span>pframe.plot(x, y, color=3D'k', xlabel=3D'x axis= 9;, ylabel=3D'y axis', theme=3D'dark')<br></div><div class= =3D"gmail_default" style=3D"font-family:arial,sans-serif"><br></div><div cl= ass=3D"gmail_default" style=3D"font-family:arial,sans-serif">but there is s= ome ambiguity in setting the theme and trace color -- the default is to obe= y the explicitly set colors, so that would draw a black trace on a dark bac= kground.</div><div class=3D"gmail_default" style=3D"font-family:arial,sans-= serif"><br></div><div class=3D"gmail_default" style=3D"font-family:arial,sa= ns-serif">If you want to set the theme programmatically,=C2=A0 it would be = "PlotPanel.conf.set_theme('seaborn-bright')" or "Plo= tFrame.panel.conf.set_theme('matplotlib')".=C2=A0=C2=A0</div><= div class=3D"gmail_default" style=3D"font-family:arial,sans-serif"><br></di= v><div class=3D"gmail_default" style=3D"font-family:arial,sans-serif">And, = as a general rule you can force a redraw with "PlotPanel.canvas.draw()= ".</div><div class=3D"gmail_default" style=3D"font-family:arial,sans-s= erif"><br></div></div><div dir=3D"ltr" class=3D"gmail_signature"><div dir= =3D"ltr">--Matt=C2=A0<br></div></div></div> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups &= quot;wxPython-users" group.<br /> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:[email protected]">wxpy= [email protected]</a>.<br /> To view this discussion on the web visit <a href=3D"https://groups.google.c= om/d/msgid/wxpython-users/CA%2B7ESboq5Eq74NikcXu_OxJ1r%3DuWXuFs4XBkUX%2BFEZ= SzkXLxMg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">https://g= roups.google.com/d/msgid/wxpython-users/CA%2B7ESboq5Eq74NikcXu_OxJ1r%3DuWXu= Fs4XBkUX%2BFEZSzkXLxMg%40mail.gmail.com</a>.<br /> --000000000000b59d5505d9a3aad3--