How can you prevent axis labels from having uneven spacing from the axis

I have some simple test code to plot an array and label the Y-axis:
x = randi(10,40); % random matrix
ax1 = axes;
imagesc(x) % plot matrix as image
labels = {'Some - M','Thing - M', 'A - M', 'ABC - M'}; % some random test labels for Y-axis
set(ax1,'YTickLabel',labels,'YTick',1:2:40,'FontSize',8); % setting up Y-axis ticks and labels
I then save the image using [file>export setup>fonts] and I untick custom size and tick custom name and select Helvetica as the font; I then save it as a vector graphic file (.svg).
The problem: The Y-axis labels having uneven gaps from the Y-axis, as you can see in the image below, they don't line up with the red line.
I think it is something to do with using a non-monospaced font, as when I use a monospaced font such as Courier, the axis labels line up perfectly:
Is there a way to get the labels to line up perfectly when using a non-monospaced font such as Helvetica?
If not, are there any monospaced fonts in MATLAB that look similar to Helvetica?

답변 (1개)

Mario Malic
Mario Malic 2023년 10월 1일
Sometimes you have to let things be imperfect. Also, put down that magnifier in the drawer.

댓글 수: 5

Hi, thanks for the answer
Isn't the Horizontal Alignment property a text property as opposed to an axes property?
x = randi(10,40);
ax1 = axes;
imagesc(x)
labels = {'Some - M','Thing - M', 'A - M', 'ABC - M'};
set(ax1,'YTickLabel',labels,'YTick',1:2:40,'FontSize',8,'HorizontalAlignment','right');
Error using matlab.graphics.axis.Axes/set
Unrecognized property HorizontalAlignment for class Axes.
So as far as I can tell I can't use this property unless I want to manually add text annotations.
Also don't the Y-axis labels already align to the right by default (just not perfectly)?
Try
ax1.YLabel.HorizontalAlignment = "right"
Geith
Geith 2023년 10월 1일
편집: Geith 2023년 10월 1일
I tried that, it ran fine with no error. However, I end up with the same result as my original post.
So it seems that the labels are already horizontally aligned to the right by default, they just dont seem to line up properly when the font is non-monospaced.
Honestly, it's barely noticeable to me, and probably your other viewers, especially when it's shown with a normal size on a normal monitor. Personally I wouldn't worry about it.
I know it seems petty but on my actual code it is definitely more noticable, I just gave this simple code as an easy example to demonstrate the problem without 100 lines of code.
Here is a small section of how it looks on my actual work - it's not terrible but defintely more noticable:
Anyways, I guess I will have to ignore it since there doesn't seem to be a solution anyways, apart from using a monospaced font. I was just asking to see if there was perhaps another solution.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 10월 1일

댓글:

2023년 10월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by