Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot bar in matlab with log-scale x axis and same width

Tags:

plot

matlab

I want to plot a bar chart in Matlab with (1) log-scale for x-axis and (2)bars in same width. But with the code below, the width of the bars are different. Can any one help? Many thanks!

    xdata = [0.01 0.018 0.032 0.056 0.1 0.18 0.32 0.56 1 1.8 3.2 5.6 10];
    ydata = [1.3 1.6 1.5 1.2 1.0 3.5 0.6 3.1 1.6 1.9 1.7 0.3 0.4];
    bar(xdata,ydata);
    set(gca,'XScale','log');

enter image description here

like image 722
user2230101 Avatar asked Mar 14 '14 16:03

user2230101


People also ask

How do you plot a function with a log scale in Matlab?

Plot Multiple Lines Create a vector of x-coordinates and two vectors of y-coordinates. Plot two lines by passing comma-separated x-y pairs to loglog . Alternatively, you can create the same plot with one x-y pair by specifying y as a matrix: loglog(x,[y1;y2]) .

How do you plot a line and bar graph in Matlab?

Plot a bar chart using the left y-axis. Plot a line chart using the right y-axis. Assign the bar series object and the chart line object to variables. days = 0:5:35; conc = [515 420 370 250 135 120 60 20]; temp = [29 23 27 25 20 23 23 17]; yyaxis left b = bar(days,temp); yyaxis right p = plot(days,conc);

What is bar graph in Matlab?

bar( y ) creates a bar graph with one bar for each element in y . To plot a single series of bars, specify y as a vector of length m. The bars are positioned from 1 to m along the x-axis. To plot multiple series of bars, specify y as a matrix with one column for each series.


1 Answers

Instead of plotting xdata on a log scale, plot the log of xdata on a linear scale. Then modify labels to reflect the linear value (not the used log value).

xdata = [0.01 0.018 0.032 0.056 0.1 0.18 0.32 0.56 1 1.8 3.2 5.6 10];
ydata = [1.3 1.6 1.5 1.2 1.0 3.5 0.6 3.1 1.6 1.9 1.7 0.3 0.4];
bar(log10(xdata),ydata);
set(gca,'Xtick',-3:1); %// adjust manually; values in log scale
set(gca,'Xticklabel',10.^get(gca,'Xtick')); %// use labels with linear values

enter image description here

like image 132
Luis Mendo Avatar answered Oct 27 '22 22:10

Luis Mendo