Reputation: 1316
I am trying to adjust the scale of the x-axis so that the values are closer together, but I am not able to do so.
I need the output to be like this photo:
However, what I actually get is the photo below:
Here's the code I have written to reproduce this error:
x = [0.1 1 10 100 1000 10000];
y = [1.9904 19.8120 82.6122 93.0256 98.4086 99.4016];
figure;
bar(x,y);
ylabel('Y values');
xlabel('X values');
set(gca,'XTick', [0.1 1 10 100 1000 10000])
How can I adjust the x-axis so that it looks like the first photo?
Upvotes: 1
Views: 4898
Reputation: 104565
Because your data has such huge dynamic range, and because of the linear behaviour of the x
axis, your graph is naturally going to appear like that. One compromise that I can suggest is that you transform your x
data so that it gets mapped to a smaller scale, then remap your x
data so that it falls onto a small exponential scale. After, simply plot the data using this remapped scale, then rename the x
ticks so that they have the same values as your x
data. To do this, I would take the log10
of your data first, then apply an exponential to this data. In this way, you are scaling the x
co-ordinates down to a smaller dynamic range. When you apply the exponential to this smaller range, the x
co-ordinates will then spread out in a gradual way where higher values of x
will certainly make the value go farther along the x
-axis, but not too far away like you saw in your original plot.
As such, try something like this:
x = [0.1 1 10 100 1000 10000]; %// Define data
y = [1.9904 19.8120 82.6122 93.0256 98.4086 99.4016];
xplot = (1.25).^(log10(x)); %// Define modified x values
figure;
bar(xplot,y); %// Plot the bar graph on the modified scale
set(gca,'XTick', xplot); %// Define ticks only where the bars are located
set(gca,'XTickLabel', x); %// Rename these ticks to our actual x data
This is what I get:
Note that you'll have to play around with the base of the exponential, which is 1.25 in what I did, to suit your data. Obviously, the bigger the dynamic range of your x
data, the smaller this exponent will have to be in order for your data to be closer to each other.
From your comments, you want the bars to be equidistant in between neighbouring bars. As such, you simply have to make the x
axis linear in a small range, from... say... 1 to the total number of x
values. You'd then apply the same logic where we rename the ticks on the x
axis so that they are from the true x
values instead. As such, you only have to change one line, which is xplot
. The other lines should stay the same. Therefore:
x = [0.1 1 10 100 1000 10000]; %// Define data
y = [1.9904 19.8120 82.6122 93.0256 98.4086 99.4016];
xplot = 1:numel(x); %// Define modified x values
figure;
bar(xplot,y); %// Plot the bar graph on the modified scale
set(gca,'XTick', xplot); %// Define ticks only where the bars are located
set(gca,'XTickLabel', x); %// Rename these ticks to our actual x data
This is what I get:
Upvotes: 4