python - scatter plot with dates and 3rd variable as color
<p>I am trying to plot an x-y plot, with x or y as date variable, and using a 3rd variable to color the points.
I managed to do it if none of the 3 variables are date, using:</p>

<pre><code>ax.scatter(df['x'],df['y'],s=20,c=df['z'], marker = 'o', cmap = cm.jet )
</code></pre>

<p>After searching, I find out that for normal plot, we have to use plot_date(). Unfortunately, I haven't been able to color the points.
Can anybody help me?</p>

<p>Here is a small example:</p>

<pre><code>import matplotlib, datetime
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas as pd

todayTime=datetime.datetime.now();
df = pd.DataFrame({'x': [todayTime+datetime.timedelta(hours=i) for i in range(10)], 'y': range(10),'z' : [2*j for j in range(10)]});
xAlt=[0.5*i for i in range(10)];

fig, ax = plt.subplots()
ax.scatter(df['x'],df['y'],s=20,c=df['z'], marker = 'o', cmap = cm.jet )
plt.show()
</code></pre>

<p>You can replace df['x'] by xAlt to see the desired result</p>

<p>Thank you</p>

<p>As far as I know, one has to use <code>scatter</code> in order to color the points as you describe. One workaround could be to use a <code>FuncFormatter</code> to convert the tick labels into times on the x-axis. The code below converts the dates into numbers, makes the scatter plot, and uses a <code>FuncFormatter</code> to convert the tick labels back into dates.</p>

<pre><code>import matplotlib, datetime
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas as pd
from matplotlib.ticker import FuncFormatter

todayTime=datetime.datetime.now()
df = pd.DataFrame({'x': [todayTime+datetime.timedelta(hours=i) for i in  range(10)], 'y': range(10),'z' : [2*j for j in range(10)]})

def my_formatter(x, pos=None):
    d = matplotlib.dates.num2date(x)
    if len(str(d.minute)) == 1:
        mn = '0{}'.format(d.minute)
    else:
        mn = str(d.minute)
    if len(str(d.hour)) == 1:
        hr = '0{}'.format(d.hour)
    else:
        hr = str(d.hour)
    return hr+':'+mn

major_formatter=FuncFormatter(my_formatter)

nums = np.array([matplotlib.dates.date2num(di) for di in df['x']])

fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(major_formatter)
ax.scatter(nums,df['y'],s=20,c=df['z'], marker = 'o', cmap = cm.jet )
xmin = df['x'][0]-datetime.timedelta(hours=1)
xmax = df['x'][len(df['x'])-1]+datetime.timedelta(hours=1)
ax.set_xlim((xmin,xmax))
plt.show()
</code></pre>

python date matplotlib scatter