跳转到主要内容

在Matplotlib中绘制样式化文本

项目描述

PyPI - Version Build Status Code style: black codecov

简介

Flexitext是一个Python库,它使得在Matplotlib中绘制具有多种样式的文本变得更容易。这个库受到了R包ggtext的启发和影响。

安装

Flexitext需要一个可工作的Python解释器(3.7+)。此库可以使用pip安装

pip install flexitext

或者,您可以从GitHub安装开发版本

pip install git+https://github.com/tomicapretto/flexitext.git

Flexitext仅需要Matplotlib版本3.4或更高。

概述

尽管受到了ggtext的启发,但Flexitext并不使用HTML、CSS或Markdown来指定文本样式。相反,它实现了一种基于标签的样式化,看起来类似于HTML标签,但并不完全像HTML。这些格式化字符串由三个部分组成

  • 一个打开标签,定义要应用的风格。
  • 要样式的文本。
  • 一个关闭标签,指示打开标签中的样式应用的范围。

让我们看看一个例子

"<color:blue, size:16>This is blue text</> and this is regular text"
  • <color:blue, size:16>是打开标签。样式由冒号分隔的键值对组成。多个样式由逗号分隔。
  • This is blue text是文本块。这段文本将使用16号字体和蓝色颜色绘制。
  • </>是关闭标签。只有打开和关闭标签之间的文本被格式化。

最后,我们有和这是普通文本。这将使用默认样式绘制,因为它不包含任何格式化标签。

示例

使用flexitext的最简单方法是通过flexitext函数。

import matplotlib as mpl
import matplotlib.pyplot as plt

from flexitext import flexitext

mpl.rcParams['figure.facecolor'] = 'w'
fig, ax = plt.subplots(figsize=(9, 6))

text = "Normal text"
ax.text(0.5, 0.7, text, size=24, ha="center")

text = "<weight:bold, size:24>Bold text</>"
flexitext(0.5, 0.6, text, ha="center")

text = "<style:italic, size:24>Italic text</>"
flexitext(0.5, 0.5, text, ha="center")

text = "<weight:bold, size:24>Bold and</> <style:italic, size:24>italic too!</>"
flexitext(0.5, 0.4, text, ha="center");

png

样式可以嵌套

fig, ax = plt.subplots(figsize=(9, 6))

text = "<size:28, color:royalblue>It is much <weight:bold>easier </><style:italic>now</></>"
flexitext(0.5, 0.6, text, ha="center");

png

一个更复杂的例子

text = (
    "<size:28, color:blueviolet, name:Montserrat>You can write using\n</>"
    "<color:grey, size:24>multiple formats,\nand linebreaks\n\n"
    "<color:royalblue, name:Montserrat>also <weight:bold>bold text\n\n</></>"
    "<name:Montserrat>and why not <color:royalblue, style:italic>italics</> too</></>"
)

fig, ax = plt.subplots(figsize=(9, 6))
flexitext(0.5, 0.5, text, ha="center", ma="center");

png

使用图形分数坐标来编写格式化标题。

fig, ax = plt.subplots(figsize=(9, 6))
fig.subplots_adjust(top=0.8, left=0.025)

x = [1, 2, 3]
y_blue = [2, 2.7, 4.5]
y_red = [1, 3, 2.5]


ax.scatter(x, y_blue, color="royalblue", s=120)
ax.scatter(x, y_red, color="crimson", s=120)

# Add flexitext
text = (
    "<name:Montserrat><size:24>A <weight:bold>great chart</> showing</>\n"
    "<size:18>the values for the "
    "<color:royalblue, weight:bold>blues</> and the <color:crimson, weight:bold>reds</></></>"
)
flexitext(0.025, 0.8, text, va="bottom", xycoords="figure fraction");

png

备注

Flexitext仅支持以下样式

  • alpha
  • 背景颜色
  • 颜色
  • 字体族
  • 名称
  • 大小
  • 样式
  • 粗细

有关它们的意义和可用值的信息,请参阅Matplotlib的文档

Flexitext标志是用Flexitext和Matplotlib制作的(见这里)。

相关工作

  • highlight_text:Flexitext和highlight_text具有类似的目标。这个库highlight_text允许您自定义高亮文本的更多方面,例如文本的边界框或具有路径效果的文本边框。另一方面,它要求您将样式作为分离的字典列表传递,而不是在文本中。

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定要选择哪一个,请了解更多关于安装包的信息。

源分布

flexitext-0.2.0.tar.gz (12.5 kB 查看散列

上传时间

构建分布

flexitext-0.2.0-py3-none-any.whl (15.3 kB 查看散列

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面