0
votes

xarray.plot.line allows you to draw 2d graphs from your x-array. However the lines have a specific colormap which i don't know how to change.

I want to create the plot with a custom ListedColormap instead of the default one.

Example

I don't want the lines to be blue, orange and green

data = xr.DataArray(np.random.randn(2, 3),dims=('x', 'y'),coords={'x': [10, 20]})

>

<xarray.DataArray (x: 2, y: 3)>
array([[ 1.44058444,  0.41396068,  0.08280716],
       [ 1.0605549 , -0.97921245, -0.22373267]])
Coordinates:
  * x        (x) int64 10 20

Dimensions without coordinates: y

>

data.plot.line(x="x")

plotted lines

since xarray.plot.line doens't take a "cmap" argument I don't know how to change the colors.

Thanks for any help!

1
You could set a color cycle, either directly via matplotlib or via seaborn. You need to set this a the start, before the ax has been created. Either way, you need a list of colors, not a colormap.JohanC

1 Answers

0
votes

Thank you @JohanC, cycler worked!

import xarray as xr
import matplotlib.pyplot as plt
from cycler import cycler

colorlist = ['c', 'm', 'y', 'k']

cmap = mpl.colors.ListedColormap(colorlist)

custom_cycler = cycler(color=cmap.colors) #or simply color=colorlist

fig = plt.figure()
ax = plt.gca()

ax.set_prop_cycle(custom_cycler)

data = xr.DataArray(np.random.randn(2, 3),dims=('x', 'y'),coords={'x': [10, 20]})
data.plot.line(ax=ax, x="x");

enter image description here