英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论