Skip to content Skip to sidebar Skip to footer

How To Plot Time Series Heatmap With Python?

I want to draw a chart with x-axis as timeline, y-axis as its value and the color will indicate its frequency. The higher the frequency, the deeper the color is.

Solution 1:

I think you are looking for a 2d histogram:

import matplotlib.pyplot as plt

plt.hist2d(x, y)

The default plot is not as pretty as your example, but you can play with it and change the colormap, bins, ...

Edit:

This produces a plot much closer to your example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

# example data
x = np.linspace(0, 10, 10000)
y = 0.5*x+np.random.randn(10000)

# make a custom colormap with transparency
ncolors = 256
color_array = plt.get_cmap('YlOrRd')(range(ncolors))
color_array[:, -1] = np.linspace(0, 1, ncolors)
cmap = LinearSegmentedColormap.from_list(name='YlOrRd_alpha', colors=color_array)

plt.hist2d(x, y, bins=[15, 30], cmap=cmap, edgecolor='white')
plt.show()

The result is: enter image description here

I hope this is helpful.

Post a Comment for "How To Plot Time Series Heatmap With Python?"