gpt4 book ai didi

python - 如何创建具有裁剪背景的渐变文本

转载 作者:行者123 更新时间:2023-12-05 03:19:04 24 4
gpt4 key购买 nike

我正在尝试绘制渐变文本。我的想法是使用代码 from the docs 创建渐变。创建渐变背景,然后使用文本路径剪辑该图像。无论出于何种原因,我只收到第一个字母和第二个字母。

enter image description here

更改轴的限制或图形的宽度似乎不起作用。我可以更改 TextPath 的大小,但我似乎无法让文本占据图像的完整大小。将文本大小设置为 .2 并将 ylim 从 0 设置为 .2 只会使文本变小。如何使文本跨越轴的大小并采用整个渐变范围?

enter image description here

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib.textpath
import matplotlib.patches
import numpy as np


def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
"""
Draw a gradient image based on a colormap.

Parameters
----------
ax : Axes
The axes to draw on.
extent
The extent of the image as (xmin, xmax, ymin, ymax).
By default, this is in Axes coordinates but may be
changed using the *transform* keyword argument.
direction : float
The direction of the gradient. This is a number in
range 0 (=vertical) to 1 (=horizontal).
cmap_range : float, float
The fraction (cmin, cmax) of the colormap that should be
used for the gradient, where the complete colormap is (0, 1).
**kwargs
Other parameters are passed on to `.Axes.imshow()`.
In particular useful is *cmap*.
"""
phi = direction * np.pi / 2
v = np.array([np.cos(phi), np.sin(phi)])
X = np.array([[v @ [1, 0], v @ [1, 1]],
[v @ [0, 0], v @ [0, 1]]])
a, b = cmap_range
X = a + (b - a) / X.max() * X
im = ax.imshow(X, extent=extent, interpolation='bicubic',
vmin=0, vmax=1, **kwargs)
return im


fig, ax = plt.subplots(figsize=(1,1))

im = gradient_image(ax, direction=1, extent=(0, 1, 0, 1), transform=ax.transAxes,
cmap=plt.cm.winter, cmap_range=(0.2, 0.8), alpha=0.5)

fp = FontProperties(family='DejaVu Sans', weight='bold')
text = matplotlib.textpath.TextPath((0.0, 0.0), 'ABCDEFG',
size=1, prop=fp)

im.set_clip_path(text, transform=ax.transAxes)

# ax.set_xticks(())
# ax.set_yticks(())
# ax.tick_params(width=0, which='both')
# ax.set_xlim((0, 10))
# ax.set_ylim((0, 1))
for spine in ['top', 'bottom', 'left', 'right']:
ax.spines[spine].set_visible(False)

最佳答案

我不是很流利 matplotlib transformations ,但据我所读,ax.transData 似乎是更好的方法(评论是我更改了您的代码):

enter image description here

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib.textpath
import matplotlib.patches
import numpy as np


def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
"""
Draw a gradient image based on a colormap.

Parameters
----------
ax : Axes
The axes to draw on.
extent
The extent of the image as (xmin, xmax, ymin, ymax).
By default, this is in Axes coordinates but may be
changed using the *transform* keyword argument.
direction : float
The direction of the gradient. This is a number in
range 0 (=vertical) to 1 (=horizontal).
cmap_range : float, float
The fraction (cmin, cmax) of the colormap that should be
used for the gradient, where the complete colormap is (0, 1).
**kwargs
Other parameters are passed on to `.Axes.imshow()`.
In particular useful is *cmap*.
"""
phi = direction * np.pi / 2
v = np.array([np.cos(phi), np.sin(phi)])
X = np.array([[v @ [1, 0], v @ [1, 1]],
[v @ [0, 0], v @ [0, 1]]])
a, b = cmap_range
X = a + (b - a) / X.max() * X
# added origin = lower, elsewise text is flipped upside down
im = ax.imshow(X, extent=extent, interpolation='bicubic',
vmin=0, vmax=1, origin='lower', **kwargs)
return im


fig, ax = plt.subplots(figsize=(1, 1))

# define text before gradient to get extent
fp = FontProperties(family='DejaVu Sans', weight='bold')
text = matplotlib.textpath.TextPath((0.0, 0.0), 'ABCDEFG',
size=1, prop=fp)
# use text to define imshow extent
extent = text.get_extents().extents[[0, 2, 1, 3]]
im = gradient_image(ax, direction=1, extent=extent,
cmap=plt.cm.winter, cmap_range=(0.2, 0.8), alpha=0.5)

# use transData instead of transAxes
im.set_clip_path(text, transform=ax.transData)
# © trenton
ax.spines[['top', 'bottom', 'left', 'right']].set_visible(False)

plt.show()

关于python - 如何创建具有裁剪背景的渐变文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73548805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com