Plot with linspace: display x-tick values not index

  • Thread starter skynelson
  • Start date
  • Tags
    Index Plot
In summary, the conversation discusses changing the tick values on the x-axis of a graph to display along the range of 0 to 1 instead of the default range of 1 to 1000. The suggested solution involves including the vector W in the plot command and using MATLAB's syntax to change the tick values and labels.
  • #1
skynelson
58
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
  • #2
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
 
  • #3
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 skynelson
  • #4
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.
 
  • #5
Thanks hdp12 for the response! This is a good approach for more specific control over layout.
 
  • Like
Likes hdp12

1. What is linspace and how does it relate to plotting?

Linspace is a function in programming languages, particularly in MATLAB, that creates a linearly spaced vector of values. It is commonly used in plotting to generate a set of evenly spaced values along an axis.

2. How can I display x-tick values instead of index values using linspace?

To display x-tick values instead of index values, you can use the 'xticklabels' argument in the plot function. Set the argument to the desired x-tick values, which can be generated using the linspace function.

3. Can linspace be used for non-linearly spaced values?

No, linspace is specifically designed for creating linearly spaced values. If you need to generate non-linearly spaced values for plotting, you can use other functions such as logspace or linspace with a transformation function.

4. How can I change the number of x-tick values displayed when using linspace?

You can use the 'xticks' argument in the plot function to specify the number of x-tick values to display. This argument takes in an array of values, which can be generated using linspace, to determine the positions of the x-tick values.

5. Is linspace limited to plotting only in MATLAB?

No, linspace is a commonly used function in many programming languages, including Python, Julia, and R. It can be used for various purposes, including plotting, in these languages.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
563
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
880
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top