I'm using xarray in python to plot a DataArray with several panels containing each of them plotting several lines (hue). By default, the location of the legend is set on the right of the panels. I'm trying to find a way to move it to the top or bottom of the panels.
I have no issue doing that with plot.pcolormesh, as the argument cbar_kwargs={'orientation': 'horizontal'} works well, but does it exist something similar with plot.line()? I've looked at the documentation and I couldn't find anything regarding that.
As an example, this is code I use (it is very straightforwards):
p = dataArray.plot.line(
y="z",
hue="source",
col="t",
col_wrap=4,
)
plt.gca().invert_yaxis()
plt.draw()
plt.show()
Which produces this figure.
Thanks for your help.
Edit. Following advises from somewhere else, I explored a bit the problem.
Xarray creates a FacetGrid object (from seaborn lib). This object contains a function to automatically add a legend (which is used by xarray) to expand the figure with a right panel where the legend object is drawn. I figured out how to access this object (p.figlegend) which itself has the attribute set_bbox_to_anchor which indeed allows to move the legend... but only relative to this additional panel.
Now that I have the legend object, I could totally move it where ever I want, but this is not really the option I was looking for. Firstly, if I do that I will have to create the legend object from scratch, which is what I wanted to avoid by using xarray automatic line plot, secondly I will have a small empty panel on the right of my figure. At this point creating the figure from subplot & making the panels one by one would be easier.
I think my issue could be simply reduced to: is it possible to have a FacetGrid object that draw the legend under or over the figure instead of on the right? If not, there is probably no simple way to do what I want to do.