
Plot Outputs in Shiny for Python || Winston Chang || RStudio
Shiny makes it easy to build interactive web applications with the power of Python’s data and scientific stack. You can try out Shiny for Python without installing a single thing… All in the browser. Learn more about Shiny for Python: https://shiny.rstudio.com/py/ Check out our interactive Shiny for Python examples: https://shinylive.io/py/examples/ Content: Winston Chang (@winston_chang)
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
So this is in again in the examples page and if we scroll down there's a section that demonstrates different types of outputs and so one of the outputs is plot output and this is a really basic example once again so our UI is very similar we have an input slider and this time we have an output plot and it's named plot but you know just for clarity I'll call it my plot just so that that's not confusing now for this example we also needed to import matplotlib and numpy to generate some random data so the code is pretty similar to what we had before we have this output decorator and we have a render plot decorator this time instead of render text and it has some alt text which will display if you hover over the plot and then we have the function that actually generates the plot now these first two lines here this is just some numpy code to generate random numbers and then this is a matplotlib histogram so we just say plt.hist we give it the data that we generated and for this particular function the second parameter is how many bins are in the histogram so that's basically like how wide each of these bars is so if I make it five there's only there's five of these bars if I make it a hundred there's a hundred of them so and that's all that it really takes to to create a plot in Shiny you just say render.plot and then if you're using matplotlib you can say plt.hist I know Shiny also currently supports Seaborn and Plotline and it also supports other plotting libraries that that work in the browser so for example it supports Plotly and Altair and those are supported via the Shiny widgets package but I'm not gonna go over those right now but just just so you know those you to use those you don't use render plot there's there's a different function for those and there we'll have examples for that as well.
Using a real dataset with palmer penguins
okay so if we want to plot like an actual data set instead of just this fake histogram there's a great example data set in a package called palmer penguins and let's import that like this from palmer penguins import load underscore penguins okay now well one cool thing that you can do with shinylive is that I can actually execute this line of code so I'm on a Mac so I already hit command to enter and it'll execute in the terminal on a Windows machine or Linux you'd press ctrl enter but I'll just run it like that so this palmer penguins package is available it's just available and I and if I run load penguins down here it'll it'll run it in this Python console and print it out here so so we can see that there's a this is a data frame it's got 344 rows and eight columns and it's got data about the about various penguin specimens that were in the wild.
okay so let's let's say we want to take this data set and and make a scatter plot of some of the measurements that we took of these penguins so let's get rid of this all this stuff here we're gonna do this differently and in fact this time we're also going to use the Seaborn package for plotting so let's say import Seaborn as SNS and then I also think we don't we don't need mempy we don't need matplotlib all right so this time let's we're gonna do different kind of plot here so let's say we'll do penguins equals load penguins and now we want to make a plot of this so I actually happen to have done this already this is like in the cooking show where you put you know you put the pan in one oven and you take it out of the other one so this is this is the code here for this plot and let's add a couple other things so in this case I've created I've used Seaborn to create this this plot object and then and then I'm gonna return that plot object previously with matplotlib what I did was I just had it do the plotting command instead of returning a matplotlib figure object and that's another way that you can do it but in this case I'm gonna have it return the object.
okay so if I run this it's gonna take a moment to load Seaborn I think and there we have our scatterplot and this is a scatterplot for the three different species of penguins so that's that's what's happening here huge species and we've also fit these linear model lines or these yeah these linear model fit lines to each subset of the data now the slider doesn't actually do anything right now so I could take it out or I could figure out what I want to do with it.
Adding an interactive species selector
so let's say we just want to look at one species at a time so instead of looking at all of them all at once so right now we have this slider input that doesn't actually do anything so let's get rid of that and let's add something that lets us select a particular species to look at so let's say UI dot input you can do how about input select so give us a selection box so we can say this is which species you want to look at and say which species is your favorite and then we can give it a list of choices so I'm just gonna go to the next line we can say a deli which I hope that's how to pronounce it gentoo and chinstrap okay and then we can put a comma there and another cool thing about this online editor is you can just click this button and it'll reformat the code theory there we go it expects that it to be kind of wide that the editor be kind of wide.
okay so that gives us a rerun this application and then we can select a particular species of penguin and now again this is not hooked up to anything it's just it's just you've just added the UI component so in order to make this actually work let's say let's take a subset of this data so we can say penguins equals ones where we can do it this way when species equals all right so this is gonna take a subset for just one species and let's rerun this app so this is just showing us the data for a deli penguins and if we change it it's showing us the data for gentoo penguins and chinstrap.

