繁体   English   中英

如何使用 matplotlib 添加左边距和右边距?

[英]How can I add a left and a right margin with matplotlib?

你好我有这个代码:

import matplotlib.pyplot as plt
from matplotlib import gridspec


Figure = plt.figure()
gs = gridspec.GridSpec(2, 3)
ax0 = Figure.add_subplot(gs[0, 0])
ax0.axis('off')
ax1 = Figure.add_subplot(gs[0, 1])
ax1.axis('off')
ax2 = Figure.add_subplot(gs[0, 2])
ax2.axis('off')
ax1.text(.5, .5, ('{}\n\n{}, {}').format("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
         "Hello", "2020-07-12"),
         color="white", style='oblique', ha='center', va='center', wrap=True,
         bbox={'facecolor': "#34C8EC", 'boxstyle': 'round,pad=1'})
Figure.canvas.draw_idle()
plt.show()

问题如下:

我的阴谋

但我想在左右两边留出边距,这意味着蓝色框和边框之间有一个空间。

我该怎么做才能做到这一点?

非常感谢 !

有趣的问题。 我查看了matplotlib.text模块的源代码,发现了一个 function _get_wrap_line_width() ,它似乎负责计算框的宽度。 但是除了文本的 position 及其方向之外,您不能向它传递任何参数。 没有保证金。 我的结论是,你想做的事情是不可能的。

我可以提出的唯一解决方法是将文本手动包装在源代码中,如下所示:

ax1.text(.5, .5, ('{}\n\n{}, {}').format(
"""Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the
1500s, when an unknown printer took a galley of type and scrambled it to make
a type specimen book. It has survived not only five centuries, but also the leap
into electronic typesetting, remaining essentially unchanged. It was popularised
in the 1960s with the release of Letraset sheets containing Lorem Ipsum
passages, and more recently with desktop publishing software like Aldus
PageMaker including versions of Lorem Ipsum.""",
    "Hello", "2020-07-12"),
    color="white", style='oblique', ha='center', va='center', wrap=True,
    bbox={'facecolor': "#34C8EC", 'boxstyle': 'round,pad=1'})

要知道在哪里插入中断,我发现将框暂时向左移动很有用,例如ax1.text(.4, .5, ...

暂无
暂无

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

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