0

I have some time series data that I want to plot, and I want to use color scaling to represent the age of a datapoint. I can successfully get the colour bar working:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.dates import date2num

x = np.random.randint(low=0, high=10, size=10)
y = np.random.randint(low=0, high=10, size=10)
dates = pd.date_range("2000-01-01", end="2000-01-10", freq="D")

df = pd.DataFrame({"x": x, "y": y, "dates": dates})

sc = plt.scatter(df["x"], df["y"], c=[date2num(i) for i in df["dates"]])
plt.colorbar(sc)
plt.savefig("dates.png")

However, I don't understand how to use the built in colour maps, like the perceptually uniform or sequential colour maps. How do I use a built in colour map as c when calling scatter?

Sienna
  • 1,570
  • 3
  • 24
  • 48
  • 1
    Does adding `cmap` option to `scatter`, e.g. `sc = plt.scatter(df["x"], df["y"], c=[date2num(i) for i in df["dates"], cmap=plt.cm.viridis)` do what you want? If you are pre-matplotlib 1.5 then check out this answer http://stackoverflow.com/questions/32484453/how-to-use-viridis-in-matplotlib-1-4 to get the Perceptually Uniform Sequential colourmaps – Ed Smith Jan 16 '17 at 08:49
  • That did exactly what I was hoping it would! Thanks! If you want to answer the question with that comment, I'll accept it. – Sienna Jan 16 '17 at 19:56
  • Done, although I think is is probably a duplicate of http://stackoverflow.com/questions/6063876/matplotlib-colorbar-for-scatter#6065493 – Ed Smith Jan 17 '17 at 08:20

1 Answers1

0

Adding the cmap option to scatter: sc = plt.scatter(df["x"], df["y"], c=[date2num(i) for i in df["dates"], cmap=plt.cm.viridis). You can specify the range of the colorbar with the vmin and vmax arguments.

If you are pre-matplotlib 1.5 then check out this answer to get the Perceptually Uniform Sequential colourmaps.

Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55