matplotlib中的粗体注释文本

时间:2016-03-22 18:30:44

标签: python matplotlib

我想使用matplotlib在绘图中制作带注释的文本。我尝试过以下方法:

 a=10.0
 font=matplotlib.font_manager.FontProperties()
 font.set_weight('bold')
 text(0,1,":.2f".format(a), fontproperties=font)

我也尝试过:

 a=10.0
 text(0,1,":.2f".format(a), weight='bold')

他们都没有工作。

最小例子:

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版本:1.2.1

python 3.3.2

1 个答案:

答案 0 :(得分:4)

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'))

无粗体注释

plt.annotate('12 largest counties; most vote for Obama', xy=(650000, 52),
             xytext=(400000, 35), fontsize=10, arrowprops=dict(arrowstyle="->", color='b'))

weight='bold'

注释
plt.annotate('small counties; most vote for McCain', xy=(50000, 20), weight='bold',
             xytext=(150000, 7), fontsize=10, arrowprops=dict(arrowstyle="->", color='r'))

plt.show()

最终图片:

enter image description here