防止Matplotlib删除坐标轴上的数字的方法

huangapple go评论92阅读模式
英文:

A way to prevent matplotlib to remove the numbers on the axis

问题

我正在绘制16个子图,每个子图中都有一个值,就像这样:
(第一个子图的示例)

fig, ax = plt.subplots(4, 4)
ax[0, 0].scatter(C_CH_metalicities, C_CH_X_over_Fe, color="gray", s=3)  # 行=0,列=0(数百个值)
ax[0, 0].scatter(J072022_met, J072022_dict["C H"], marker="*", s=200)  # (1个值)

在添加单个数据值之前,我的图看起来像这样:沿y轴的良好值
之后像这样:沿y轴不好的值
问题出在y轴上的值。我希望它保持在添加单个值之前的状态。

我尝试手动设置y轴刻度,例如:

ax[0, 0].set_yticks([-1, 0, 1, 2, 3])

但这只会在y值的位置显示“-”,而不显示数字。

我知道这是一个很无聊的问题,但这让我很烦恼。

英文:

I am plotting a sample in 16 subplots together with one value in each of the plots, like this:
(example of first subplot)

fig, ax = plt.subplots(4, 4)
ax[0, 0].scatter(C_CH_metalicities,C_CH_X_over_Fe, color = "gray", s = 3) #row=0, col=0 (100s of values)
ax[0,0].scatter(J072022_met,J072022_dict["C H"], marker = "*", s = 200) #(1 value)

Before the addition of the single data value, my plot looks like this:Nice values along the yaxis
And after like this:Not nice values along the yaxis
The problem is the values on the yaxis. I want it to remain the same as it was before I added the single value.

I tried manually setting the yticks like:

ax[0,0].set_yticks([-1,0,1,2,3])

But this only gives a "-" in the positions of the yvalues, but does not show the number.

I know this is a very boring question, but it is doing my head in.

答案1

得分: 0

ax.set_yticks()的第一个参数是刻度位置,但不是标签。你只需添加labels参数。

ax[0,0].set_yticks([-1,0,1,2,3], labels=[-1,0,1,2,3])
英文:

The first argument of ax.set_yticks() is where the tick locations are, but not how they are labeled. You just need to add in the labels parameter.

ax[0,0].set_yticks([-1,0,1,2,3], labels = [-1,0,1,2,3])

huangapple
  • 本文由 发表于 2023年7月3日 21:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定