简体   繁体   English

Matplotlib“ savefig”为pdf,文本覆盖

[英]Matplotlib “savefig” as pdf, text overlay

If i run this code in python: 如果我在python中运行此代码:

titles = ctf01[0,1:] 

fig = plt.figure(figsize=(11.69,8.27), dpi=100)

for num in range(len(titles)):

    ax = fig.add_subplot(3,4,num+1)

    ax.plot(ctf03[1:,num+1], ctf0102[:,num], 'ro')

    ax.set_title(titles[num])

plt.tight_layout()

fig.text(0.5, 0.04, 'CTF12', ha='center')

fig.text(0.04, 0.5, 'CTF3', va='center', rotation='vertical')

fig.savefig("example.pdf")

i get this in the pdf file: 我在pdf文件中得到这个:

在此处输入图片说明

I would like to fix the problem with the "figure title" shown in the red circles. 我想用红色圆圈中显示的“图形标题”解决此问题。 If i set the 0.04 value as an negative value the title runs out of paper. 如果我将0.04值设置为负值,则标题用完纸。

I also would like to save some space with moving the title of the subplots (green circles) into the diagram. 我还想通过将子图的标题(绿色圆圈)移到图中来节省一些空间。 Any idea how i can realize this? 任何想法我怎么能意识到这一点?

Thanks for help. 感谢帮助。

try to add before fig.savefig("example.pdf") following line. 尝试在以下行之前在fig.savefig("example.pdf")之前添加。

plt.tight_layout()

you have it in your script but it should come after text 您在脚本中有它,但是它应该在文本之后

It looks like you're trying to set the x and y labels for the whole figure, which isn't possible as these can only be set on an Axes object. 似乎您正在尝试为整个图形设置x和y标签,这是不可能的,因为它们只能在Axes对象上设置。 Fortunately we can work around it by creating an 'invisible' subplot that fills the whole area and set the labels on this. 幸运的是,我们可以通过创建一个“不可见”子图来解决该问题,该子图填充整个区域并在其上设置标签。

After plotting your subplots you would create the invisible one with: 绘制子图后,您可以使用以下方法创建不可见的图:

label_ax = fig.add_subplot(111, frameon=False)

The frameon argument prevents it from drawing the box that is added by the default style. frameon参数可防止其绘制默认样式添加的框。 Then you tell it not to draw tick marks and make the tick labels invisible (we can't just remove them as it will mess up the spacing). 然后,您告诉它不要绘制刻度线并使刻度线标签不可见(我们不能只是删除它们,因为这样会弄乱间距)。

label_ax.tick_params(bottom=False, left=False, labelcolor="none")

Finally, set your labels: 最后,设置标签:

label_ax.set_xlabel("CTF12")
label_ax.set_ylabel("CTF3")

You can adjust the vertical positioning of the plot titles by providing a pad argument to the set_title function. 您可以通过为set_title函数提供pad参数来调整图标题的垂直位置。 Giving a negative value will push the title into the plot, you'll need trial and error to find the value that works. 赋予负值会将标题推入情节,您需要反复试验才能找到有效的值。

Putting it all together (with made-up data): 全部放在一起(与虚构数据一起):

fig = plt.figure(figsize=(11.69, 8.27), dpi=100)
for i in range(10):
    ax = fig.add_subplot(3, 4, i + 1)
    ax.plot([1, 2, 3, 4, 5], "ro")
    ax.set_title("Plot {}".format(i), pad=-15)

label_ax = fig.add_subplot(111, frameon=False)
label_ax.tick_params(bottom=False, left=False, labelcolor="none")
label_ax.grid(False)  # In case the current style displays a grid.
label_ax.set_xlabel("CTF12")
label_ax.set_ylabel("CTF3")

fig.tight_layout()
fig.savefig("example.pdf")

Which gives: 这使: 具有图形轴标签的子图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM