Issue
I am trying to make a Bar plot in python so that the color of each bar is set based on the magnitude of that value. For example the list below is our numbers:
List = [2,10,18,50, 100, ... 500]
In this situation 2 should have the lightest color and 500 should have the darkest color values. The picture below which is from excel shows what i mean:
I appreciate answers which helps me. Thank you.
Update:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
names= ['A', 'B', 'C', 'D']
counts = [30, 40, 55, 100]
bar_colors = ['blue']
ax.bar(names, counts, color=bar_colors)
ax.set_ylabel('Frequency')
ax.set_title('Names')
plt.show()
Solution
Then you can use a Colormap (extra docs) from matplotlib
In this example the bar_colors
list is being creating with a scale between 0 and 100 but you can choose the scale as you want.
import matplotlib.pyplot as plt
import matplotlib.colors
fig, ax = plt.subplots()
names= ['A', 'B', 'C', 'D', 'E']
counts = [10, 30, 40, 55, 100]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","orange", "green"])
bar_colors = [cmap(c/100) for c in counts]
ax.bar(names, counts, color=bar_colors)
ax.set_ylabel('Frequency')
ax.set_title('Names')
plt.show()
Please, notice you can instantiate the cmap
with a list of colors as you desire.
Also, be sure that when invoking cmap()
you just can use a float between [0,1].
Answered By - fernandezr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.