Matplotlib子批次yaxis比例与绘图ab重叠

2024-10-01 19:27:11 发布

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

我试着画出3个子图,它们之间没有任何空白。默认的y轴勾选标签使用显示在y轴右上角的比例(在下面的示例中为1e-8),除了下面两个与上面的图重叠的绘图之外,其他都可以。有人知道怎么解决这个问题吗?下面是一个小例子。在

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator

x = np.arange(0,200)
y = np.random.rand(200) * 10e-8


fig = plt.figure(figsize=(10,15))
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
ax0a = plt.subplot(gs1[0, :])
ax0b = plt.subplot(gs1[1, :])
ax0c = plt.subplot(gs1[2, :])


ax0a.set_xticklabels([])
ax0b.set_xticklabels([]) 

ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))

plot

所以一个解决方案是使用mtick

^{pr2}$

但我更希望能将刻度向左移动,这样它就在轴之外,如果可能的话。在


Tags: importmatplotlibasnppltgs1setsubplot
2条回答

我有两个选择你可能想看看。在

首先,按如下方式设置轴位置和大小:

# your imports and data above
fig = plt.figure()
ax0a = fig.add_axes([0.1, 0.1, 0.8, 0.25])
ax0b = fig.add_axes([0.1, 0.39, 0.8, 0.25], sharex=ax0a)
ax0c = fig.add_axes([0.1, 0.68, 0.8, 0.25], sharex=ax0a)
ax0a.set_xticklabels([])
ax0b.set_xticklabels([]) 
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
plt.show()

enter image description here

第二个选项是手动调整偏移文本的位置和字体大小:

^{pr2}$

enter image description here

好吧,这可能是一个丑陋的解决方案,但你可以只是简单的高档次 下面的数据显示了幂的范围,即ax0b.plot(x, y*1e8)。 这至少对你的例子有效。在

相关问题 更多 >

    热门问题