python - 仿射变换后不同高度的字母
问题描述
我必须在不改变宽度的情况下绘制一个字符串并转换它的高度。我确实在这里找到了一个 Scale 课程。但是,我发现不同字母之间存在高度差异(见图:G,O,C高于T,H,A,并超过了下限)。
如何使所有字母的高度和开头完全相同y_axis
?
我尝试使用txt1.get_window_extent()
来获取字符串bbox的坐标,但是仿射变换后bbox没有缩放(灰色矩形)。
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib.patheffects
# import matplotlib.patches as patches
#-----------------------------------------------------------
class Scale(matplotlib.patheffects.RendererBase):
def __init__(self, sx, sy = None):
self._sx = sx
self._sy = sy
def draw_path(self, renderer, gc, tpath, affine, rgbFace):
affine = affine.identity().scale(self._sx, self._sy) + affine
renderer.draw_path(gc, tpath, affine, rgbFace)
#-----------------------------------------------------------
N=12
fig = plt.figure(figsize = (N+1, 4))
ax = fig.add_subplot(111)
font = FontProperties()
font.set_size(80)
font.set_weight('bold')
font.set_family('monospace')
bbox_props = dict(boxstyle = "square, pad = 0.0", fill = 0, lw = 1, alpha = 0.5)
ax.plot((0.0, 1.0), (0.1, 0.1), linestyle = '--') # dashed line
ax.plot((0.0, 1.0), (0.9, 0.9), linestyle = '--')
txt1 = ax.text(0.3, 0.1, 'GOTCHA',
fontproperties = font,
ha = 'center',
va = 'baseline',
bbox = bbox_props)
txt2 = ax.text(0.8, 0.1, 'GOTCHA',
fontproperties = font,
ha = 'center',
va = 'baseline',
color = 'blue',
bbox = bbox_props)
txt1.set_path_effects([Scale(1.0, 3.0)]) # 3X in height
ax.set_ylim(0.0, 1.0)
plt.savefig('test.png')
解决方案
推荐阅读
- javascript - 从另一个选项卡返回时幻灯片太快
- android - 每 10-15 秒在后台使用 GPS 是否是一个好习惯?
- python-3.x - 为什么导入“来自 tensorflow.train 导入功能”不起作用
- spring - SpringBoot 持久化具有关系的实体
- javascript - routerlink = "functionName()" 在页面加载时立即调用
- python - 基于空间维度去除图像噪声
- angular - Angular RXJS 多个 HTTP 嵌套请求
- angular - Angular Material2对话框的父组件是什么
- elasticsearch - 弹性搜索连接查询
- php - PhpStorm 远程调试:无法找到请求目标的有效证书路径