rendering multiple variables from an xarray dataset? #39
-
Thanks for all this work and sharing it openly - it's really appreciated! I have a very basic question which I've been unable to answer by reading the docs and trying things out. Is it possible to render more than one variable in an xarray dataset? For example, something like this which would ideally animate 12 monthly frames each with two line plots on them, one for each variable reduced along a single latitude:
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Thanks for your interest in StreamJoy! To answer your question, I'm not exactly sure I follow. If lat/lon is reduced to 0-dim, I think you could create a pandas dataframe and melt, and finally groupby.
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@ahuang11 - I'm trying to follow your something like:
but I get an error like:
any thoughts on how to get a multi-variable multi-subplot to animate? Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hmm I think it's an issue I need to fix / release. import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
from streamjoy import stream, wrap_matplotlib
@wrap_matplotlib()
def renderer(ds):
fig = plt.figure(figsize=(16, 9)) # Set the figure size to be wider
fig.suptitle("Currents N/S & E/W", fontsize=16) # Add main title
ax1 = fig.add_subplot(121) # First subplot
ax2 = fig.add_subplot(122) # Second subplot
# Create a colormap and set the 'bad' color for NaN values
cmap = plt.cm.RdBu.copy()
cmap.set_bad(color="grey") # Set NaN color to black
# Plot the data on the first subplot
ds.sel(variable="air").plot(ax=ax1, cmap=cmap, cbar_kwargs={"extend": "neither"})
# Plot the data on the second subplot
ds.sel(variable="airx2").plot(ax=ax2, cmap=cmap, cbar_kwargs={"extend": "neither"})
ax2.set_title("") # Remove the title from the second plot
ax2.set_ylabel("") # Remove the x axis label from the second plot
return fig
ds = xr.tutorial.open_dataset("air_temperature")
ds["airx2"] = ds["air"] * 2
stream(
ds.to_array(),
renderer=renderer,
dim="time",
uri="currents.gif",
threads_per_worker=1,
) |
Beta Was this translation helpful? Give feedback.
A bit convoluted, but it gets the job done! The benefit of this is you can also customize how you want the plot.
Underneath, the renderer essentially just does
So what I did was first explore how to get that plot through basic…