我正在try 将两个文本框并排放在matplotlib图形的右上角.我关注了this tutorial on text-alignment,可以在右上角放置一个文本框.但我不知道如何添加第二个文本框,以使左上框的右边缘与右上框的左边缘相交.

MWE Figure

输出运行最小工作示例的代码如下:

import numpy as np
import matplotlib.pyplot as plt

## data
x = np.linspace(
    -10,
    10,
    51)
shrink_factors = np.linspace(
    1,
    0,
    x.size)
y1 = shrink_factors * np.sin(
    np.exp(
        -1 * x))
y2 = shrink_factors * np.cos(
    np.exp(
        -1 * x))

## get plot parameters
xlim = [
    np.min(x),
    np.max(x)]
ylim = [
    0,
    1.125 * np.max([
        np.max(y1),
        np.max(y2)])]
facecolors = (
    "red",
    "blue")
(color1, color2) = facecolors
label1 = "Label 1"
label2 = "Label 2"
text1 = "RED 1"
text2 = "BLUE 2"
text_background_color = "gainsboro"
text_size = 12
figsize = (12, 7)
# figsize = (7, 12)

## initialize plot
fig, ax = plt.subplots(
    figsize=figsize)

## plot data
ax.plot(
    x,
    y1,
    color=color1,
    label=label1)
ax.plot(
    x,
    y2,
    color=color2,
    label=label2)
ax.grid(
    color="black",
    linestyle=":",
    alpha=0.3)
ax.set_xlim(
    xlim)
ax.set_ylim(
    ylim)
fig.legend(
    mode="expand",
    loc="lower center",
    ncol=2)

## add text-boxes side-by-side
text_box1 = ax.text(
    0.95,
    0.95,
    text1,
    color=color1,
    fontsize=text_size,
    horizontalalignment="right",
    verticalalignment="top",
    transform=ax.transAxes)
text_box1.set_bbox({
    "facecolor" : text_background_color,
    "edgecolor" : "black"})
text_box1_pos = text_box1.get_position()
text_box2 = ax.text(
    text_box1_pos[0],
    0.95,
    text2,
    color=color2,
    fontsize=text_size,
    horizontalalignment="left",
    verticalalignment="top",
    transform=ax.transAxes)
text_box2.set_bbox({
    "facecolor" : text_background_color,
    "edgecolor" : "black"})

## finish plot
plt.show()
plt.close()

推荐答案

不久前,我也遇到过类似的问题.这就是我所做的.这是基于另一篇文章here中提供的一些信息.下面是对您代码的更新...

  1. 将第一个文本框的x位置更新为0.9,这样两个标签都在图中
  2. 绘制第一个文本框后,添加以下代码.这与我提到的帖子中的解释类似.请注意,我使用的是transAxes,因此我们在任何地方都有相同的坐标.更多信息here.注意,bb_datacoords现在将拥有第一个文本框的x和y坐标
#text_box1_pos = text_box1.get_position()  --> You code, not required anymore

r = fig.canvas.get_renderer()
transf = ax.transAxes.inverted()
bb = text_box1.get_window_extent(renderer = r)
bb_datacoords = bb.transformed(transf)
  1. 使用BBox中的x1位置作为第二个文本框的起点,如下所示...
    bb_datacoords.x1+0.01, #text_box1_pos[0],

注意,正如在上面的回答中提到的,在绘图完成之前不知道确切的位置,所以可以添加一个0.01的小增量来使它们不重叠.如果你想在两个盒子之间看到一个小的间隙,你可以增加它(比如0.02).

得到的结果是这样的.希望这能帮上忙.

enter image description here

Python相关问答推荐

如何将我的位置与光强度数据匹配到折射图案曲线中?

使用GEKKO在简单DTE系统中进行一致初始化

如何使用SubProcess/Shell从Python脚本中调用具有几个带有html标签的参数的Perl脚本?

当多个值具有相同模式时返回空

Django mysql图标不适用于小 case

非常奇怪:tzLocal.get_Localzone()基于python3别名的不同输出?

无法通过python-jira访问jira工作日志(log)中的 comments

log 1 p numpy的意外行为

如何在Raspberry Pi上检测USB并使用Python访问它?

如何从pandas的rame类继承并使用filepath实例化

如何让这个星型模式在Python中只使用一个for循环?

如何在BeautifulSoup/CSS Select 器中处理regex?

使用__json__的 pyramid 在客户端返回意外格式

用两个字符串构建回文

如何根据rame中的列值分别分组值

如何在验证文本列表时使正则表达式无序?

用fft计算指数复和代替求和来模拟衍射?

ModuleNotFoundError:Python中没有名为google的模块''

如何获得满足掩码条件的第一行的索引?

如何提高Pandas DataFrame中随机列 Select 和分配的效率?