assign particular colour to 2nd bar of the bar plot

조회 수: 1 (최근 30일)
RAJAT GANGADHARAN
RAJAT GANGADHARAN 2020년 2월 4일
댓글: Subhadeep Koley 2020년 2월 4일
I have a matrix of 4 elements. I plotted a bar graph. how can I assign red colour for the 2nd bar and blue for remaining 3 bars?

답변 (1개)

Subhadeep Koley
Subhadeep Koley 2020년 2월 4일
Hi, use the code below.
clc; close all;
% Define your 4-element matrix
y = [25 75 100 125];
% Plot bar graph
b = bar(y);
% Assign red colour to the second bar
b.FaceColor = 'flat';
b.CData(2,:) = [1 0 0];
customBarColor.png
Hope this helps!
  댓글 수: 2
RAJAT GANGADHARAN
RAJAT GANGADHARAN 2020년 2월 4일
it was showing an error as follows:
"No appropriate method, property, or field 'CData' for class
'matlab.graphics.chart.primitive.Bar'.
Did you mean:
>> b.XData (2, :) = [1 0 0];"
Subhadeep Koley
Subhadeep Koley 2020년 2월 4일
No I meant b.CData(2,:) = [1 0 0]; only.
Which matlab version are you using?
Also, can you show the matrix which you are trying to plot? (I mean you entire code)
It seems the built-in bar() function is being shadowed by a custom function with same name as "bar".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by