Search code examples
matlabmatlab-figurepercentageaxis-labels

How to plot percentage on x-axis?


I managed to read a large excel sheet in MATLAB, sorted the data and plotted the graph of the wanted data. The problem that I am experiencing is that I am trying to make the values of x-axis as a percentage. I have a variable of around 1000 integers which may increase as I add data. I have assigned a variable to check the length of variable so that I do not have to change it manually. The problem is that I need to assign the x-axis in terms of 10%, 20%... 100%. How can I do that without reducing data for the y-axis?

The following is the code for the x-axis:

A = round(length(data),2,'significant');
d = 0.1*A
e = 0:d:A
f = e/A*100
set(gca,'xticklabel', f)

The x-axis is showing up till 90%. The length of data is 16990, therefor A results in 17000, d results to be 1700, value of variable f is from 0 to 100 but the plot is still showing to 90%.

The image below shows the graph with the data, without adding limits for the ticks:

https://i.imgur.com/ccMDkzF.png

On the other hand, when I have changed 'ticklabels', it only reduced to the first points of the graph. Whilst when I have converted ticklabels to 'ticks', the graph is shown in full but the ticks are as shown below:

https://i.imgur.com/uZPU45S.png

What I am trying to do is convert the 18000 into 100%.


Solution

  • set(gca,'xticklabel', f)
    

    sets the tick labels. It doesn't change anything else about the graph. Thus, it assigns different labels to existing tick marks. It will confuse the readers of the graph!

    What you want to do is change the 'xlim' property, which determines the portion of the x-axis that you can see:

    set(gca,'xlim',f([1,end]))
    

    You might also want to set the ticks themselves:

    set(gca,'xtick', f)