Ben
Ben

Reputation: 163

Consistent color argument between matplotlib scatter to matplotlib plot?

I'm hoping to use matplotlib to plot inter-annual variation of monthly data (below). By passing c=ds['time.year'] in plt.scatter(), I achieve the desired outcome. However, I would like to be able to connect the points with an analogous plt.plot() call. Is this possible?

import pandas as pd
import matplotlib.pyplot as plt
import xarray as xr

# create y data 
y = []
for yr in range(10):
    for mo in range(12):
        y.append(yr+mo+(yr*mo)**2)

# create datetime vector
t = pd.date_range(start='1/1/2010', periods=120, freq='M')

# combine in DataArray
ds = xr.DataArray(y, coords={'time':t}, dims=['time'])

# scatter plot with color 
im = plt.scatter(ds['time.month'], ds.values, c=ds['time.year'])
plt.colorbar(im)

Output: enter image description here

I have tried the following, but it does not work:

plt.plot(ds['time.month'], ds.values, c=ds['time.year'])

Upvotes: 1

Views: 264

Answers (1)

JohanC
JohanC

Reputation: 80449

You can create a norm mapping the range of years to the range of colors. The norm together with the used colormap, can server as input for a ScalarMapple to create an accompanying colorbar. With the default 'viridis' colormap the code could look like:

import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
import pandas as pd
import xarray as xr

y = []
for yr in range(10):
    for mo in range(12):
        y.append(yr + mo + (yr * mo) ** 2)
t = pd.date_range(start='1/1/2010', periods=120, freq='M')
ds = xr.DataArray(y, coords={'time': t}, dims=['time'])

norm = plt.Normalize(ds['time.year'].min(), ds['time.year'].max())
cmap = plt.cm.get_cmap('viridis')
for year in range(int(ds['time.year'].min()), int(ds['time.year'].max()) + 1):
    plt.plot(ds['time.month'][ds['time.year'] == year],
             ds.values[ds['time.year'] == year],
             ls='-', marker='o', color=cmap(norm(year)))
plt.colorbar(ScalarMappable(cmap=cmap, norm=norm))
plt.xticks(range(1, 13))
plt.show()

lines with the same colors as a comparable scatter plot

Upvotes: 1

Related Questions