你好,我有这个代码:
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()
问题如下:
但是我想在右边和左边留出空白处,这意味着在蓝色框和边框之间留出一个空间。
我怎么做才能做到这一点呢?
非常感谢!
发布于 2020-07-12 04:59:45
这个问题很有趣。我查看了matplotlib.text
模块的源代码,发现了一个函数_get_wrap_line_width()
,它似乎负责计算盒子宽度。但是,除了文本的位置和方向之外,您不能向它传递任何参数。没有利润。我的结论是,你想做的事情是不可能的。
我建议的唯一解决方法是在源代码中手动包装文本,如下所示:
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, ...
。
https://stackoverflow.com/questions/62848947
复制相似问题