Home > Back-end >  Creating a custom colorbar for battery levels
Creating a custom colorbar for battery levels

Time:02-10

Using a simulation, I was able to construct a list of battery levels in percentage decreasing over time as you can see here :

enter image description here

I was already able to dispay the battery levels depending on the time using matplotlib pyplot. This list has a size of 1944 elements and I would like to know if there is a possibility to display these battery levels depending on the time using a custom colorbar from green (100% of battery) to red (around 15% of battery) to black (below 15% of battery which is critical).

Thanks in advance for your replies

CodePudding user response:

You can use a LinearSegmentedColormap.from_list() using tuples of values and colors. Here is an example using a seaborn heatmap to show how it works:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import seaborn as sns

cmap = LinearSegmentedColormap.from_list('', [(0, 'black'), (.15, 'black'), (.15, 'crimson'), (1, 'limegreen')])

fig, ax = plt.subplots(figsize=(5, 10))
sns.heatmap(np.linspace(100, 0, 30).reshape(-1, 1), annot=True, fmt='.2f', cmap=cmap, ax=ax)

using a LinearSegmentedColormap from list

PS: plt.plot() can't be used with a colormap. A curve has just one given color. You could use plt.scatter(temps_total_simulation, pourcentage_total, c=pourcentage_total, cmap=cmap, vmin=0, vmax=100) to individual points.

In matplotlib's tutorial, there is an coloring line segments with the listedcolormap

  • Related