在Matplotlib中本地样式文本

huangapple go评论72阅读模式
英文:

Locally style text in matplotlib

问题

请将以下内容翻译成中文:

"Let's say I would like matplotlib to display the following styled text in a plot:

> This should be bold, this should be italics and this should be a normal style.

How should I do? The following works...

import matplotlib.pyplot as plt
matplotlib.rcParams['text.usetex'] = True

plt.text(0, 0, r"\textbf{This should be bold}, \textit{this should be italics} ad this should be a normal style.")
plt.show()

but, apart from being overkill, is quite slow... I also know that I can make successive calls to plt.text and pass it arguments like fontweight = "bold", but this is even worse, since I would have to compute exactly at what place I should put all the boxes...

My question is actually part of a bigger problem: I would like to have, side by side, a plot and a text describing the plot. The text may contain tables and may need to be styled. For the moment, I create a figure, add two axes objects (one for the plot, one for the text) side by side but I am starting to fear that this approach will not allow me to style the text the way I want. Is there a nicer way?"

英文:

Let's say I would like matplotlib to display the following styled text in a plot:

> This should be bold, this should be italics and this should be a normal style.

How should I do? The following works...

import matplotlib.pyplot as plt
matplotlib.rcParams['text.usetex'] = True

plt.text(0, 0, r"\textbf{This should be bold}, \textit{this should be italics} ad this should be a normal style.")
plt.show()

but, apart from being overkill, is quite slow... I also know that I can make successive calls to plt.text and pass it arguments like fontweight = "bold", but this is even worse, since I would have to compute exactly at what place I should put all the boxes...

My question is actually part of a bigger problem: I would like to have, side by side, a plot and a text describing the plot. The text may contain tables and may need to be styled. For the moment, I create a figure, add two axes objects (one for the plot, one for the text) side by side but I am starting to fear that this approach will not allow me to style the text the way I want. Is there a nicer way?

答案1

得分: 1

根据matplotlib文档(https://matplotlib.org/stable/tutorials/text),可以通过两种方式来设置文本样式,一种是使用内置属性,如您提到的fontweight,另一种是使用LaTeX(这是您在代码中使用的方法)。

LaTeX更强大,所以我建议您使用它,特别是如果您需要表格等内容。

英文:

According to matplotlib documentationat https://matplotlib.org/stable/tutorials/text, the two ways you can style the text is either by using the inbuilt attributes like fontweight you mentioned, or by using LaTeX (which is what you do in your code).

LaTeX is more powerful, so I would suggest you go with it, especially if you need tables and stuff.

huangapple
  • 本文由 发表于 2023年5月26日 15:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定