无法在Matplotlib图形外部创建文本和注解

jpfvwuh4  于 2023-11-22  发布在  其他
关注(0)|答案(3)|浏览(83)

下面的Python代码使用Matplotlib生成了一个图,它在外观上非常适合我正在开发的应用程序。

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

fig, ax = plt.subplots(facecolor='lightgrey')

# Define some graph offsets
start_x = 18
start_y = 18
end_x = 24
end_y = 24

# Draw an arrow
ax.annotate('Target',
            xy = (18, 20),
            xytext = (14, 20),
            arrowprops = dict(facecolor ='red', shrink = 0.05, width = 2.5),
            ha = 'center')

# Set the limits of the plot
ax.set_xlim(start_x, end_x)
ax.set_ylim(start_y, end_y)

# Customize the grid
plt.gca().set_aspect('equal', adjustable='box') # Make the grid equal in both dimensions
ax.set_ylim(ax.get_ylim()[::-1])                # Invert the y axis
ax.xaxis.tick_top()                             # Move the X-Axis to the top

# Title
middle_x = 14+(end_x-14)/2
plt.text(middle_x, 18, "THE TITLE" + '\nSubtitle\n 1\n 2\n 3\n 4\n\n', color = 'k', fontsize = 18, ha = 'center')

plt.grid()
plt.show()

字符串
代码生成下图。

从控制台应用程序运行相同的代码(注解掉了%matplotlib inline),得到的结果不太理想。

最大化该图可以提供更多细节。

Matplotlib有一个“add_axes”函数,它允许修改画布。使用以下ax = fig.add_axes([0.5, 0, 0.5, 0.5])# [left, bottom, width, height]修改代码会生成以下内容:

如何修改代码,使从控制台应用程序生成的图形看起来像在MysterNotebook中生成的图形?

7rfyedvj

7rfyedvj1#

与其show绘图,不如直接保存它,如

plt.savefig("thing.pdf", bbox_inches="tight")

字符串
将参数bbox_inches设置为“tight”将给予您想要的结果。

5jdjgkvh

5jdjgkvh2#

您可以通过在调用subplots时设置图形大小和受约束的布局来接近。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(7, 6), facecolor='lightgrey', layout='constrained')

# Define some graph offsets
start_x = 18
start_y = 18
end_x = 24
end_y = 24

# Draw an arrow
ax.annotate('Target',
            xy = (18, 20),
            xytext = (14, 20),
            arrowprops = dict(facecolor ='red', shrink = 0.05, width = 2.5),
            ha = 'center')

# Set the limits of the plot
ax.set_xlim(start_x, end_x)
ax.set_ylim(start_y, end_y)

# Customize the grid
plt.gca().set_aspect('equal', adjustable='box') # Make the grid equal in both dimensions
ax.set_ylim(ax.get_ylim()[::-1])                # Invert the y axis
ax.xaxis.tick_top()                             # Move the X-Axis to the top

# Title
middle_x = 14+(end_x-14)/2
plt.text(middle_x, 18, "THE TITLE" + '\nSubtitle\n 1\n 2\n 3\n 4\n\n', color = 'k', fontsize = 18, ha = 'center')

plt.grid()
plt.show()

字符串


的数据
奇怪的是,当我尝试一个正方形的数字(例如figsize=(7, 7)),它并没有很好地工作。

goqiplq2

goqiplq23#

ChatGPT 4给了我一些想法,其中一个工作,虽然它失去了奇数的水平轴。
关键是添加:plt.tight_layout()
以下是完整的代码:

# Define some graph offsets
start_x = 18
start_y = 18
end_x = 24
end_y = 24

# Set the limits of the plot
ax.set_xlim(start_x, end_x)
ax.set_ylim(start_y, end_y)

# Draw an arrow
ax.annotate('Target',
            xy = (18, 20),
            xytext = (14, 20),
            arrowprops = dict(facecolor ='red', shrink = 0.05, width = 2.5),
            ha = 'center')

# Customize the grid
plt.gca().set_aspect('equal', adjustable='box') # Make the grid equal in both dimensions
ax.set_ylim(ax.get_ylim()[::-1])                # Invert the y axis
ax.xaxis.tick_top()                             # Move the X-Axis to the top

# Title
middle_x = 14+(end_x-14)/2
plt.text(middle_x, 18, "THE TITLE" + '\nSubtitle\n 1\n 2\n 3\n 4\n\n', color = 'k', fontsize = 18, ha = 'center')

#ax = fig.add_axes([0.5, 0, 0.5, 0.5])# [left, bottom, width, height]
plt.tight_layout()
plt.grid()
plt.show()

字符串


的数据

相关问题