本文介绍了如何在matplotlib中添加粗体注释文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 matplotlib 在绘图中制作带注释的文本.我尝试了以下方法:
a = 10.0font=matplotlib.font_manager.FontProperties()font.set_weight('粗体')text(0,1,":.2f".format(a), fontproperties=font)
我也尝试过:
a = 10.0text(0,1,":.2f".format(a), weight='bold')
它们都不起作用,也没有抛出错误.
最小示例:
将matplotlib.pyplot导入为plt定义主():plt.figure()plt.plot([0,1],[0,1])plt.text(0.5,0.5,"string",weight ="bold")
matplotlib 版本:1.2.1
蟒蛇 3.3.2
解决方案
- 在
seaborn
中添加注释/文本也可以使用相同的方法. - 可以使用
.text
或.annotate
指定粗体-
原始 MRE
- 当时可能存在错误,但来自 OP 的 MRE 可以正常工作.
def main():plt.figure()plt.plot([0,1],[0,1])plt.text(0.45,0.6,有重量",重量=粗体")plt.text(0.45,0.35,"with fontweight",fontweight ="bold")主要的()
I want to make an annotated text in a plot using matplotlib. I have tried the following:
a=10.0 font=matplotlib.font_manager.FontProperties() font.set_weight('bold') text(0,1,":.2f".format(a), fontproperties=font)
I have also tried:
a=10.0 text(0,1,":.2f".format(a), weight='bold')
None of them work, and no error is thrown.
minimal example:
import matplotlib.pyplot as plt def main(): plt.figure() plt.plot([0,1],[0,1]) plt.text(0.5,0.5,"string",weight="bold")
matplotlib version: 1.2.1
python 3.3.2
解决方案- Adding annotations / text also works in
seaborn
with the same methods. - Bold text can be specified with
.text
or.annotate
matplotlib.pyplot.text
- Use the
weight
orfontweight
parameter.
- Use the
matplotlib.pyplot.annotate
, which uses the samekwargs
as.text
.
Imports, DataFrame & Plot Setup
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns swing = pd.read_csv('https://assets.datacamp.com/production/repositories/469/datasets/e079fddb581197780e1a7b7af2aeeff7242535f0/2008_swing_states.csv') plt.figure(figsize=(10,10)) sns.scatterplot(x='total_votes', y='dem_share', data=swing, hue='state') plt.xlabel('total votes') plt.ylabel('% of vote for Obama') plt.xticks([x for x in range(0, 1000000, 100000)], rotation=40) plt.yticks([x for x in range(0, 100, 10)]) # Create a Rectangle patch plt.gca().add_patch(Rectangle((400000, 52), 500000, 34, linewidth=1, edgecolor='b', facecolor='none')) plt.gca().add_patch(Rectangle((0, 5), 50000, 45, linewidth=1, edgecolor='r', facecolor='none'))
Adding Annotations
.annotate
without boldplt.annotate('12 largest counties; most vote for Obama', xy=(650000, 52), xytext=(400000, 35), fontsize=10, arrowprops=dict(arrowstyle="->", color='b'))
.annotate
withweight='bold'
plt.annotate('small counties; most vote for McCain', xy=(50000, 20), weight='bold', xytext=(150000, 7), fontsize=10, arrowprops=dict(arrowstyle="->", color='r'))
.text
withweight='bold'
plt.text(600000, 20, '2008 Swing-State Counties', weight='bold') plt.show()
Final image:
Original MRE
- Perhaps there was a bug at the time, but the MRE from the OP, works without issue.
def main(): plt.figure() plt.plot([0,1], [0,1]) plt.text(0.45, 0.6, "with weight", weight="bold") plt.text(0.45, 0.35, "with fontweight", fontweight="bold") main()
这篇关于如何在matplotlib中添加粗体注释文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
-