plantcv
plantcv copied to clipboard
Use Altair for graphing data
Is your feature request related to a problem? Please describe. The current graphing package we use is good, but @dschneiderch raised some good sustainability issues and suggested we look at altair as an alternative. I did a bit of testing with our histogram plotting functions and I thought it was pretty awesome. It uses data frames, so we would mostly only need to replace the graphing functions without having to do a lot of data reengineering. Switching to altair would eliminate a lot of the warnings we get during testing.
Describe the solution you'd like
In plantcv.analyze_color
, for example, plotting all color channels would look like this:
dataset = dataset.melt(id_vars=("bins"))
hist_chart = alt.Chart(dataset).mark_line().encode(
alt.X("bins", title="Pixel intensity"),
alt.Y("value", title="Frequency counts"),
color="variable").interactive()
The chart can be either plotted or saved:
# Plot
print(hist_chart)
# Save to SVG
hist_chart.save("plot.svg")
# Save to PNG
hist_chart.save("plot.png")
To display charts the right renderer needs to be enabled. In my testing alt.renderers.enable('notebook')
works in Jupyter Notebook but alt.renderers.enable('default')
works in Jupyter Lab. There are other options for other platforms. This might make things more complicated.
Describe alternatives you've considered
We have looked at quite a few options. Our preference is to avoid packages in the matplotlib
family. plotnine
has been nice because of ggplot
-compatible syntax, but has some limitations.
the new version 4 has a new renderer that is supposed to work across the board. https://altair-viz.github.io/releases/changes.html Also, it has this interactive histogram feature that looks fairly awesome https://altair-viz.github.io/gallery/histogram_responsive.html
unfortunately i haven't gotten it to install alongside plantcv 3.8 through conda. i think there is a version incompatibility. will update if i figure it out.
@DannieSheng I know you were looking into interactive plotting! Here's another potential option to look into.