小x记号在下面,大ab

2024-09-28 12:12:28 发布

您现在位置:Python中文网/ 问答频道 /正文

X轴的短记号在Axes之下,而长记号在Axes之上,有可能吗

使用下面的代码,我可以在同一侧绘制次要记号和主要记号,但是我希望05记号位于绘图上方,而次要记号位于绘图下方

from matplotlib import figure as mpfig
from matplotlib.backends import backend_agg as mpback


fig = mpfig.Figure()
mpback.FigureCanvas(fig)
ax = fig.add_subplot(111)

x = list(range(10))
y = list(range(10))

ax.set_xticks(list(range(0, 11, 5)))
ax.set_xticks(list(range(0, 11, 1)), minor=True)

ax.plot(x, y)
fig.savefig("picture.png")

此代码生成以下图片:

enter image description here

我知道^{},我本想在那里找到这样一个选项,但没有找到。 我也知道^{}^{},但它们会将次要和主要记号移到同一侧

我想避免使用Axes.twinx的任何技巧,因为它会添加一个新的Axes对象,这会使绘图更重


Tags: fromimport绘图matplotlibasfigrangeax
1条回答
网友
1楼 · 发布于 2024-09-28 12:12:28

Axes.tick_params应该能够解决这个问题

ax.tick_params(axis='x',which='minor',top=False,bottom=True)
ax.tick_params(axis='x',which='major',top=True,bottom=False)

经过努力,这对我很有用:

from matplotlib import figure as mpfig
from matplotlib.backends import backend_agg as mpback


fig = mpfig.Figure()
mpback.FigureCanvas(fig)
ax = fig.add_subplot(111)

x = list(range(10))
y = list(range(10))

ax.tick_params(axis='x',which='minor',top=False,bottom=True)
ax.tick_params(axis='x',which='major',top=True,bottom=False)

ax.set_xticks(list(range(0, 11, 5)))
ax.set_xticks(list(range(0, 11, 1)), minor=True)

ax.plot(x, y)
fig.savefig("picture.png")

相关问题 更多 >

    热门问题