Minor Tick Marks Don't Render Consistently

5 次查看(过去 30 天)
When I adjust the size of the plot window, the minor ticks on the x-axis dissappear. You can recreate the issue with the following code, where the first figure doesn't render the minor x ticks and the second figure does.
Is there a way to force the first figure to render the ticks without changing the style of the figure?
figure(1)
x=[1 10 100];
y =[1 10 100];
plot(x,y)
axis([1e-3 1e3 1e1 1e3])
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
set(gca,'FontSize',16, 'FontName', 'arial' )
figure(2)
figure('units','normalized','position',[.1 .1 .4 .4])
x=[1 10 100];
y =[1 10 100];
plot(x,y)
axis([1e-3 1e3 1e1 1e3])
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
set(gca,'FontSize',16, 'FontName', 'arial' )

回答(2 个)

Massimo Zanetti
Massimo Zanetti 2016-10-7
Yes there is a simple way. Take control of ticks length. See last code line:
figure(1)
x=[1 10 100];
y =[1 10 100];
plot(x,y)
axis([1e-3 1e3 1e1 1e3])
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
set(gca,'FontSize',16, 'FontName', 'arial' )
%control ticks length
set(gca,'ticklength',2*get(gca,'ticklength'))
  1 个评论
bsol
bsol 2016-10-14
When I run your code, it still generates the plot without minor x ticks. The extra line does indeed make the ticks longer, but they still only appear when I resize the window. Let me know if you need more details to understand my issue.

请先登录,再进行评论。


Shubham
Shubham 2024-9-2
Hi Bsol,
The issue you're encountering with the disappearance of minor ticks when resizing the plot window is likely due to MATLAB's automatic adjustment of axis ticks based on the figure size. When the figure is too small, MATLAB may choose not to display minor ticks to prevent clutter.
To ensure that minor ticks are always displayed regardless of the figure size, you can explicitly enable them using the gca properties. Here's how you can modify your code to force the display of minor ticks:
% First figure
figure(1)
x = [1 10 100];
y = [1 10 100];
plot(x, y)
axis([1e-3 1e3 1e1 1e3])
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
set(gca, 'FontSize', 16, 'FontName', 'arial')
% Force the display of minor ticks
set(gca, 'XMinorTick', 'on', 'YMinorTick', 'on')
% Second figure
figure(2)
figure('units', 'normalized', 'position', [.1 .1 .4 .4])
x = [1 10 100];
y = [1 10 100];
plot(x, y)
axis([1e-3 1e3 1e1 1e3])
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
set(gca, 'FontSize', 16, 'FontName', 'arial')
% Force the display of minor ticks
set(gca, 'XMinorTick', 'on', 'YMinorTick', 'on')
Explanation
  • XMinorTick and YMinorTick: These properties control the display of minor ticks on the x-axis and y-axis, respectively. Setting them to 'on' ensures that minor ticks are displayed regardless of the figure size.
  • Consistency: Applying these settings to both figures ensures that minor ticks are consistently displayed across different figure sizes and styles.
By explicitly enabling minor ticks, you should be able to maintain their visibility even when the figure size is adjusted.

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by