简体   繁体   中英

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

Hello I have this code:

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()

And the problem is the following:

我的阴谋

But I would like to have margin at right and at left that's mean a space between the blue box and the border.

How can I do to do this?

Thank you very much !

Interesting question. I had a look at the source code of the matplotlib.text module and discovered a function _get_wrap_line_width() , which seems to be responsible for computing the box width. But you cannot pass it any parameters, other than the position of the text and its orientation. No margin. My conclusion is that what you want to do is impossible.

The only workaround I can propose is wrapping the text manually in the source code, like this:

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'})

To know where to insert the breaks, I found it useful to temporarily shift the box to the left, like ax1.text(.4, .5, ... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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