Vary the Color of Each Bar in Bargraph Using Particular Value

vary the color of each bar in bargraph using particular value

bar takes a list of colors as an argument (docs). Simply pass in the colors you want.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
from numpy.random import rand

fig, ax = plt.subplots(1, 1)
# get a color map
my_cmap = cm.get_cmap('jet')
# get normalize function (takes data in range [vmin, vmax] -> [0, 1])
my_norm = Normalize(vmin=0, vmax=5)
# some boring fake data
my_data = 5*rand(5)
ax.bar(range(5), rand(5), color=my_cmap(my_norm(my_data)))

plt.show()

enter image description here

Different color for each bar in a bar chart; ChartJS

After looking into the Chart.Bar.js file I've managed to find the solution.
I've used this function to generate a random color:

function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

I've added it to the end of the file and i called this function right inside the "fillColor:" under

helpers.each(dataset.data,function(dataPoint,index){
//Add a new point for each piece of data, passing any required data to draw.

so now it looks like this:

helpers.each(dataset.data,function(dataPoint,index){
//Add a new point for each piece of data, passing any required data to draw.

datasetObject.bars.push(new this.BarClass({
value : dataPoint,
label : data.labels[index],
datasetLabel: dataset.label,
strokeColor : dataset.strokeColor,
fillColor : getRandomColor(),
highlightFill : dataset.highlightFill || dataset.fillColor,
highlightStroke : dataset.highlightStroke || dataset.strokeColor
}));
},this);

and it works I get different color for each bar.

Bar with different color

The ax.bar() method returns a list of bars that you can then manipulate, in your case with .set_color():

import matplotlib.pyplot as plt

f=plt.figure()
ax=f.add_subplot(1,1,1)

## bar() will return a list of bars
barlist = ax.bar([1,2,3,4], [1,2,3,4])
barlist[3].set_color('r')

plt.show()

Change colours of particular bars in a bar chart - depending on two factors

Just replace the appropriate values in cols. This is done easily with [<- or you could use replace which is a wrapper for the same thing

Assuming you are using alpha = 0.05

myalpha <- 0.05
cols[pval > myalpha] <- 'grey' # could also be cols <- replace(cols, pvals > 0.05, 'grey')
barplot(x, col = cols, horiz=T)

enter image description here

chart.js bar chart change color based on value

Given an array of number values named "data", you create a sorted array out of it. Then you map the values of the original data, returning the appropriate color depending on its position in the sorted array.

const backgroundColors = data.map(v => sortedData.indexOf(v) >= data.length - 3 ? 'red' : 'green');

Please have a look at the runnable code sample below.

const labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O'];const data = labels.map(l => Math.floor(Math.random() * 1000) + 1);const sortedData = data.slice().sort((a, b) => a - b);const backgroundColors = data.map(v => sortedData.indexOf(v) >= data.length - 3 ? 'red' : 'green');
new Chart(document.getElementById('myChart'), { type: 'bar', data: { labels: labels, datasets: [{ label: "My Dataset", data: data, backgroundColor: backgroundColors }] }, options: { legend: { display: false }, scales: { yAxes: [{ ticks: { beginAtZero: true } }] } }});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script><canvas id="myChart" height="90"></canvas>


Related Topics



Leave a reply



Submit