python绘制表格并填充文本_python – matplotlib文本bbox的特定于边的填充

我找到了一个基于

the answer suggested in this post的解决方法.一个更简单的解决方案也将受到赞赏.我还应该说,垂直错位似乎只有在我设置usetex:True时才会发生.

这是上面的修改版本:

import matplotlib

matplotlib.rc('text',usetex=True)

import matplotlib.pyplot as plt

from matplotlib.patches import Rectangle

import numpy as np

text = '\\begin{tabular}{|c|c|}\\hline1&2\\\\\\hline3&4\\\\\\hline\\end{tabular}'

fig, ax = plt.subplots(1)

img = ax.imshow(np.zeros((10,10)), cmap=plt.cm.gray)

txt = ax.text( 4.5,

4.5,

text,

fontsize=24,

ha='center',

va='center',

bbox=dict(alpha=0))

fig.canvas.draw()

bbox = txt.get_bbox_patch()

xmin = bbox.get_window_extent().xmin

xmax = bbox.get_window_extent().xmax

ymin = bbox.get_window_extent().ymin

ymax = bbox.get_window_extent().ymax

xmin, ymin = fig.transFigure.inverted().transform((xmin, ymin))

xmax, ymax = fig.transFigure.inverted().transform((xmax, ymax))

dx = xmax-xmin

dy = ymax-ymin

# The bounding box vals can be tweaked manually here.

rect = Rectangle((xmin-0.02,ymin-0.01), dx+0.04, dy+0.05, fc='w', transform=fig.transFigure)

ax.add_patch(rect)

fig.canvas.draw()

ax.axis('off')

plt.savefig('ok.png',bbox_inches='tight')

这会产生:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Pythonmatplotlib绘制多组数据雷达图的代码: ```python import numpy as np import matplotlib.pyplot as plt # 数据 data = { "group1": [0.6, 0.7, 0.8, 0.9, 0.95], "group2": [0.2, 0.4, 0.6, 0.8, 0.9], "group3": [0.1, 0.3, 0.5, 0.7, 0.8] } # 标签 labels = ["A", "B", "C", "D", "E"] # 使用 matplotlib 创建一个子图 fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(polar=True)) # 设置角度 angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False) angles = np.concatenate((angles, [angles[0]])) # 绘制每组数据的雷达图 for i, (name, values) in enumerate(data.items()): # 将第一个值放到最后一个,以使雷达图闭合 values = np.concatenate((values, [values[0]])) # 绘制雷达图 ax.plot(angles, values, label=name) ax.fill(angles, values, alpha=0.1) # 设置雷达图的参数 ax.set_thetagrids(angles[:-1] * 180 / np.pi, labels) ax.set_ylim([0, 1]) ax.grid(True) # 添加图例 ax.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1)) # 显示雷达图 plt.show() ``` 在这个示例代码中,我们使用了一个字典来存储多组数据,每组数据都是一个列表。我们使用 `for` 循环来遍历每组数据,使用 `plot()` 方法绘制雷达图,并使用 `fill()` 方法填充图形。我们还使用了 `set_thetagrids()` 方法来设置角度和标签,使用 `set_ylim()` 方法来设置雷达图的 Y 轴范围,使用 `grid()` 方法绘制网格线,使用 `legend()` 方法添加图例。最后,使用 `show()` 方法显示雷达图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值