Plot with linspace: display x-tick values not index

  • Thread starter Thread starter skynelson
  • Start date Start date
  • Tags Tags
    Index Plot
Click For Summary
SUMMARY

This discussion focuses on customizing x-axis tick values in MATLAB plots using the linspace function. The user initially encountered an issue where the x-axis displayed index numbers from 1 to 1000 instead of the desired range of 0 to 1. The solution involves using the plot command with the linspace-generated vector W and adjusting the x-tick values and labels accordingly. The final code snippet provided effectively demonstrates how to achieve the desired x-axis formatting.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of the linspace function in MATLAB
  • Basic knowledge of plotting functions in MATLAB
  • Experience with customizing plot properties in MATLAB
NEXT STEPS
  • Explore MATLAB's plot customization options
  • Learn about MATLAB's tick mark properties and how to manipulate them
  • Investigate advanced plotting techniques in MATLAB
  • Study the use of linspace for generating data points in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and anyone involved in scientific computing who seeks to enhance their data visualization skills by customizing plot axes.

skynelson
Messages
57
Reaction score
4
I want my graph to display the tick values along the x-axis, instead of the index number.
Because I use linspace, my data goes from 0 to 1, but my index ranges from 1 to 1000.
My plot labels the x-axis from 1 to 1000, by default. I want to change the default (or just program it) to display 0 to 1, so it looks like a physics graph. Any help much appreciated, thanks!
Code:
T = 4;
W = linspace(0, 1, 1000);
plot(sin(T*2*pi*W+1E-8)./(2*pi*W + 1E-8));

labelsXaxisExample.jpg
 
Physics news on Phys.org
This looks like Matlab to me - is that correct? If so, you just include the vector W insdied the plot command

plot(W, sin(T*2*pi*W+1E-8)./(2*pi*W + 1E-8));

jason
 
Code:
A = plot(sin(T*2*pi*W+1E-8)./(2*pi*W + 1E-8));

A.Parent.XTick = [0:100:1000];
A.Parent.XTickLabel = {'0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1'};

1610980510344.png
 
  • Like
Likes   Reactions: skynelson
jasonRF said:
This looks like Matlab to me - is that correct? If so, you just include the vector W insdied the plot command

plot(W, sin(T*2*pi*W+1E-8)./(2*pi*W + 1E-8));

jason
Yes, That's it! And yes, MATLAB. I figured there was a straightforward syntactical approach. Thanks Jason.
 
Thanks hdp12 for the response! This is a good approach for more specific control over layout.
 
  • Like
Likes   Reactions: hdp12

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K