thalitus thalitus - 1 year ago 121
Python Question

python matplotlib : plot vertical_barplotcolor with classes

I'm currently looking for a tips or two to make vertical parrallel barplot (or rectangles) with differents colours according to the data.

For example, the plot of this

data = [1,1,1,2,2,1,3,3,3,2,2,2,3,3,3,3]
would be something like enter image description here

Thank you for your help :).


Answer Source

That is easily done with matplotlib. The following code exactly produces the result from above

import matplotlib.pyplot as plt
import numpy as np

data = [1,1,1,2,2,1,3,3,3,2,2,2,3,3,3,3]
col= [None, '#1620c0', '#e11239', '#2f9007' ]
colors = [col[d] for d in data]

fig = plt.figure(figsize=(5,4))
ax = fig.add_axes([0,0,1,1])
# create bar plot 
#       postions of bars   , all 1 in height   , all 1 in width    , color according to data, no line, np.ones(len(data)), np.ones(len(data)), color=colors, linewidth=0 )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download