Change Axis of a 2D plot in which the numbers have a power
1 次查看(过去 30 天)
显示 更早的评论
Hello,
Figure 1 was obtained using meshgrid function. The problem now is that it was required to me to change the axis of the plot for a better understanding (center of the axis at the center of the domain). To get a little closer to this goal, I used the following code which produced figure 2:
set(gca,'XTick',[0.006:0.001:0.012])
set(gca,'XTickLabel',['-3';'-2';'-1';' 0';' 1';' 2';' 3'])
set(gca,'YTick',[0.006:0.001:0.012])
set(gca,'YTickLabel',['-3';'-2';'-1';' 0';' 1';' 2';' 3'])
The problem with this method is that the positive numbers are not centered and that I am unable to put the power 10^-3 on both axis.
Can anybody give me a hint or a better way to deal with this question?
Thank you very much!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/152591/image.png)
0 个评论
回答(1 个)
Star Strider
2015-12-12
I don’t entirely understand what ‘not centred’ means. It would be helpful if you described what you want.
It would be easier to define the tick locations and tick labels using the linspace function rather than the colon operator in this particular situation. The advantage of linspace is that it allows you to easily specify the length of the vector returned, with a different ‘step’ increment between values.
I can understand your wanting to shift the x-axis location to centre it, but power is power. I would leave the y axis as it is. If you want the y-axis to be divided by 1000, you have to do that when you define your 'YTickLabel' values.
2 个评论
Star Strider
2015-12-12
My pleasure.
First, with respect to the axis labels, I would use the new round function to round them to E-3. If you don’t have it, you can use the ‘roundn’ anonymous function version that works the same way.
To get the axes with integer labels and the exponents labeled separately, multiply the axis tick labels by 1000 and use a text object to label the exponents. This would be something like:
roundn = @(x,n) round(x .* 10.^n)./10.^n; % Round ‘x’ To ‘n’ Digits, Emulates Latest ‘round’ Function
xtl = roundn(linspace(-3/1000,3/1000,7), 3);
ytl = roundn(linspace(-3/1000,3/1000,7), 3);
[X,Y] = meshgrid([0.006:0.001:0.012]);
M = 1-((X-0.01).^2 + (Y-0.009).^2); % Create Data
figure(1)
contourf(X,Y,M)
set(gca,'XTick',[0.006:0.001:0.012]);
set(gca,'XTickLabel',xtl*1E3)
text(0.0117, 0.0054, '\cdot10^3')
set(gca,'YTick',[0.006:0.001:0.012]);
set(gca,'YTickLabel',ytl*1E3)
text(0.006, 0.0123, '\cdot10^3')
It works with the sample code here. You may have to experiment with it in your code to get the result you want.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!