I want to produce in python with matplotlib/pyplot
while keeping module dependencies at a minimum.
Is there something simpler than:
import matplotlib.pyplot as plt
def color_gradient ( val, beg_rgb, end_rgb, val_min = 0, val_max = 1):
val_scale = (1.0 * val - val_min) / (val_max - val_min)
return ( beg_rgb[0] + val_scale * (end_rgb[0] - beg_rgb[0]),
beg_rgb[1] + val_scale * (end_rgb[1] - beg_rgb[1]),
beg_rgb[2] + val_scale * (end_rgb[2] - beg_rgb[2]))
# -----------------------------------------------
x_lbls = [ "09:00", "09:15", "10:10"]
y_vals = [ 7, 9, 5]
plt_idx = np.arange( len( x_lbls))
bar_wd = 0.35
grad_beg, grad_end = ( 0.5, 0.5, 0.5), (1, 1, 0)
col_list = [ color_gradient( val,
grad_beg,
grad_end,
min( y_vals),
max( y_vals)) for val in y_vals]
plt.bar( plt_idx, y_vals, color = col_list)
plt.xticks( plt_idx + bar_wd, x_lbls)
plt.show()
this is still missing the legend color bar
my solution in R with ggplot would be:
library(ggplot2)
df = data.frame( time = 1:10, vals = abs(rnorm( n = 10)))
ggplot( df, aes( x = time, y = vals, fill = vals)) +
geom_bar(stat = "identity") +
scale_fill_gradient(low="#888888",high="#FFFF00")
and produces the desired output:
You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.
there is no color parameter listed where you might be able to set the colors for your bar graph.
You can also set the color individually for each point/bar if you change the data array to be configuration objects instead of numbers.
I couldn't figure out how to get the colorbar to work without plotting something else and then clearing it, so it's not the most elegant solution.
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
y = np.array([1, 4, 3, 2, 7, 11])
colors = cm.hsv(y / float(max(y)))
plot = plt.scatter(y, y, c = y, cmap = 'hsv')
plt.clf()
plt.colorbar(plot)
plt.bar(range(len(y)), y, color = colors)
plt.show()
You can use Normalize and ScalarMappable without plotting a scatter. For example:
import matplotlib mpl
import matplotlib.pyplot as plt
from matplotlib import cm
f,(ax1,ax2) = plt.subplots(2)
#ax1 --> plot here your bar chart
norm = mpl.colors.Normalize(vmin=0, vmax=1)
mpl.colorbar.ColorbarBase(ax2, cmap=cm.RdBu,
norm=norm,
orientation='horizontal')
Finally, add the desired format to the colorbar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With