2

I'm using xarray.DataArray.plot to plot my values, such as:

In [26]: cla()

In [27]: da = xarray.DataArray(arange(6*5).reshape(6,5), dims=("a", "b"), coords={"a": range(6), "b": range(5), "c": ("a", range(6))})

In [28]: da["c"].plot(label="a")
Out[28]: [<matplotlib.lines.Line2D at 0x7fef942e8b38>]

In [29]: da["c"].plot(label=None)
Out[29]: [<matplotlib.lines.Line2D at 0x7fef94279dd8>]

In [30]: legend()
Out[30]: <matplotlib.legend.Legend at 0x7fef942cba20>

My desired effect is that the da["c"].plot(label=None) does not result in any labelled line. However, xarray appears to give it a label "c" in the resulting plot, whereby it appears in the legend. How can I suppress this?

Resulting plot

gerrit
  • 24,025
  • 17
  • 97
  • 170

2 Answers2

2

Xarray's plotting module seems to skip the legend entry if you pass an empty string:

da["c"].plot(label="a")
da["c"].plot(label='')
legend()

enter image description here

jhamman
  • 5,867
  • 19
  • 39
0

One workaround is to set the label after the line has been plotted, following this answer:

In [46]: L = da["c"].plot(label=None)    

In [47]: L[0].set_label(None) 
gerrit
  • 24,025
  • 17
  • 97
  • 170