gpt4 book ai didi

python - 包装文本在 matplotlib 中不起作用

转载 作者:行者123 更新时间:2023-11-28 18:16:23 25 4
gpt4 key购买 nike

我正在尝试使用 wrap=True 来换行文本,但它似乎对我不起作用。从下面的 matplotlib 运行示例:

import matplotlib.pyplot as plt

fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = "This is a really long string that I'd rather have wrapped so that it"\
" doesn't go outside of the figure, but if it's long enough it will go"\
" off the top or bottom!"
plt.text(4, 1, t, ha='left', rotation=15, wrap=True)
plt.text(6, 5, t, ha='left', rotation=15, wrap=True)
plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
va='top', wrap=True)
plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)
plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)

plt.show()

给我这个:

文本换行错误

enter image description here

有什么问题吗?

最佳答案

Matplotlib 硬连线使用图形框作为环绕宽度。要解决此问题,您必须重写 _get_wrap_line_width 方法,该方法返回线条在屏幕像素中的长度。例如:

text = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ')
txt = ax.text(.2, .8, text, ha='left', va='top', wrap=True,
bbox=dict(boxstyle='square', fc='w', ec='r'))
txt._get_wrap_line_width = lambda : 600. # wrap to 600 screen pixels

lambda 函数只是一种创建函数/方法的简单方法,无需使用 def 编写命名函数。

显然使用私有(private)方法会带来风险,例如它会在未来的版本中被删除。而且我还没有测试过它与轮换的配合情况。如果你想做一些更复杂的事情,比如使用数据坐标,你必须继承 Text 类,并显式覆盖 _get_wrap_line_width 方法。

import matplotlib.pyplot as plt
import matplotlib.text as mtext

class WrapText(mtext.Text):
def __init__(self,
x=0, y=0, text='',
width=0,
**kwargs):
mtext.Text.__init__(self,
x=x, y=y, text=text,
wrap=True,
**kwargs)
self.width = width # in screen pixels. You could do scaling first

def _get_wrap_line_width(self):
return self.width

fig = plt.figure(1, clear=True)
ax = fig.add_subplot(111)

text = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ')

# Create artist object. Note clip_on is True by default
# The axes doesn't have this method, so the object is created separately
# and added afterwards.
wtxt = WrapText(.8, .4, text, width=500, va='top', clip_on=False,
bbox=dict(boxstyle='square', fc='w', ec='b'))
# Add artist to the axes
ax.add_artist(wtxt)

plt.show()

关于python - 包装文本在 matplotlib 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48079364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com