首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用matplotlib添加左边距和右边距?

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

Stack Overflow用户
提问于 2020-07-11 20:06:51
回答 1查看 98关注 0票数 0

你好,我有这个代码:

代码语言:javascript
运行
复制
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()

问题如下:

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

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

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2020-07-12 04:59:45

这个问题很有趣。我查看了matplotlib.text模块的源代码,发现了一个函数_get_wrap_line_width(),它似乎负责计算盒子宽度。但是,除了文本的位置和方向之外,您不能向它传递任何参数。没有利润。我的结论是,你想做的事情是不可能的。

我建议的唯一解决方法是在源代码中手动包装文本,如下所示:

代码语言:javascript
运行
复制
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, ...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62848947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档