MATLAB中不显示刻度修改X轴范围及数据点可见性问题
Hey there! Let's work through your two MATLAB questions step by step:
The trick here is to split the task into two parts: first set your desired X-axis range, then explicitly define which ticks you want to show (hiding any new ticks that come with the expanded range). Here are two common scenarios:
Scenario 1: Expand X-axis range but keep only original data ticks
Suppose your data spans x=1:8, but you want the X-axis to show from 0 to 9 without displaying ticks for 0 or 9. Use this code:
% After your plotting code xlim([0 9]); % Set your custom X-axis range xticks(1:8); % Only show ticks for your original data points
Scenario 2: Hide all X-axis ticks entirely
If you want to adjust the X-axis range and show no ticks at all (labels and tick marks), use:
xlim([your_min your_max]); xticks([]); % This removes all tick marks and labels
You can also add xticklabels([]) for extra safety, though xticks([]) usually handles both.
Looking at your data, the 5th and 6th points have the same y value (80), so their markers overlap completely. Here are three straightforward fixes:
Fix 1: Slightly offset the X positions of overlapping points
This moves the points just enough to separate them visually, while keeping the X-axis ticks aligned with your original feature counts:
x = 1:8; x(5) = 4.8; % Shift 5th point left slightly x(6) = 5.2; % Shift 6th point right slightly y = [70 74 77 78 80 80 82 83]; err = [14 11 12 10 10 7 7 5]; % Add markers and increase their size for better visibility errorbar(x,y,err,'red','marker','o','markersize',8); ylabel('Classification results','fontsize',20); xlabel('Nr of features used', 'fontsize',20); title('Feature selection with Genetic Algorithm','fontsize',24); ylim([55 100]); xticks(1:8); % Keep original X ticks even with shifted points
Fix 2: Use different markers for overlapping points
If you don't want to shift X positions, use distinct marker shapes to tell the points apart:
x = 1:8; y = [70 74 77 78 80 80 82 83]; err = [14 11 12 10 10 7 7 5]; % Plot all points first with a base marker errorbar(x,y,err,'red','marker','o','markersize',6); hold on; % Overplot the two overlapping points with unique markers errorbar(x(5),y(5),err(5),'red','marker','s','markersize',8); % Square marker errorbar(x(6),y(6),err(6),'red','marker','^','markersize',8); % Triangle marker hold off; ylabel('Classification results','fontsize',20); xlabel('Nr of features used', 'fontsize',20); title('Feature selection with Genetic Algorithm','fontsize',24); ylim([55 100]);
Fix 3: Increase marker size and add distinct colors
Make the markers larger and use different face/edge colors to make overlapping points stand out:
errorbar(x,y,err,'red','marker','o','markersize',10,'markerfacecolor','white','markeredgewidth',2); % For the overlapping points, change the face color hold on; errorbar(x(5),y(5),err(5),'red','marker','o','markersize',10,'markerfacecolor','blue','markeredgewidth',2); hold off;
All these methods will make the two overlapping points easy to distinguish without altering your core data.
内容的提问来源于stack exchange,提问作者Pugl

