Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

matplotlib - change color graphic bar matlab

I have two datasets of data printed on the same graph, what I want is to differentiate the information by putting a legend that indicates the color of each of the two pieces of information in the graph. I cannot solve this, that is, I have not been able to put a different color to each information and also a legend that shows what data each color belongs to.1

My code

         .
         .
         .  
         %grafica de barras 1
         bar(app.CasosUIAxes,data1.dateRep,data1.cases,'r')
        
        %grafica de barras 2
        bar(app.CasosUIAxes,data2.dateRep,data2.cases)
       
        

        %personalizacion de la grafica
        text = strcat('Casos por día en:',{' '}, pais1,{' '},'vs',{' '},pais2) ;
        legend(app.CasosUIAxes,text,'Location',"northwest")
        xlabel(app.CasosUIAxes,'Fecha')
        ylabel(app.CasosUIAxes,'Cantidad de casos')
        axis(app.CasosUIAxes,'tight')
        xtickangle(app.CasosUIAxes,90)
        grid(app.CasosUIAxes,"on")
         .
         .
         .
         .

enter image description here

You can only differentiate the lighter bars from the dark ones, the idea is to change that color to a color, for example a red and black. At the same time in the legend show the indications of these two colors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this, bar(app.CasosUIAxes,data1.dateRep,data1.cases, 'r') bar(app.CasosUIAxes,data2.dateRep,data2.cases, 'b')

For adding legend to bar graphs,

b1 = bar(x, y1);

hold on

b2 = bar(x, y2);

legend([b1 b2],'Bar Chart 1','Bar Chart 2')

Source:: Matlab Documentation Page. (link)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...