Reputation: 2181
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
Upvotes: 49
Views: 161043
Reputation: 62403
seaborn
axes-level plots with the same methods.
seaborn
figure-level plots, you must iterate through each axes, which isn't shown..text
or .annotate
matplotlib.pyplot.text
weight
or fontweight
parameter.matplotlib.pyplot.annotate
, which uses the same kwargs
as .text
.plt.rc('text', usetex=True)
, are used, weight='bold'
may have no effect. In this case, use the LaTex syntax \textbf{}
(e.g. plt.text(600000, 20, r'\textbf{2008 Swing-State Counties}')
)python 3.11.2
, pandas 2.0.1
, matplotlib 3.7.1
, seaborn 0.12.2
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(range(0, 1000000, 100000), rotation=40)
plt.yticks(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'))
.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
with 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'))
.text
with weight='bold'
plt.text(600000, 20, '2008 Swing-State Counties', weight='bold')
plt.show()
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()
fig, ax = plt.subplots(figsize=(10, 10))
sns.scatterplot(x='total_votes', y='dem_share', data=swing, hue='state', ax=ax)
ax.set_xlabel('total votes')
ax.set_ylabel('% of vote for Obama')
ax.set_xticks(range(0, 1000000, 100000), range(0, 1000000, 100000), rotation=40)
ax.set_yticks(range(0, 100, 10))
# Create a Rectangle patch
ax.add_patch(Rectangle((400000, 52), 500000, 34, linewidth=1, edgecolor='b', facecolor='none'))
ax.add_patch(Rectangle((0, 5), 50000, 45, linewidth=1, edgecolor='r', facecolor='none'))
ax.annotate('12 largest counties; most vote for Obama', xy=(650000, 52),
xytext=(400000, 35), fontsize=10, arrowprops=dict(arrowstyle="->", color='b'))
ax.annotate('small counties; most vote for McCain', xy=(50000, 20), weight='bold',
xytext=(150000, 7), fontsize=10, arrowprops=dict(arrowstyle="->", color='r'))
ax.text(600000, 20, '2008 Swing-State Counties', weight='bold')
plt.show()
g = sns.relplot(x='total_votes', y='dem_share', data=swing, hue='state', height=10)
# iterate through each axes of the figure-level plot
for ax in g.axes.flat:
ax.set_xlabel('total votes')
ax.set_ylabel('% of vote for Obama')
ax.set_xticks(range(0, 1000000, 100000), range(0, 1000000, 100000), rotation=40)
ax.set_yticks(range(0, 100, 10))
# Create a Rectangle patch
ax.add_patch(Rectangle((400000, 52), 500000, 34, linewidth=1, edgecolor='b', facecolor='none'))
ax.add_patch(Rectangle((0, 5), 50000, 45, linewidth=1, edgecolor='r', facecolor='none'))
ax.annotate('12 largest counties; most vote for Obama', xy=(650000, 52),
xytext=(400000, 35), fontsize=10, arrowprops=dict(arrowstyle="->", color='b'))
ax.annotate('small counties; most vote for McCain', xy=(50000, 20), weight='bold',
xytext=(150000, 7), fontsize=10, arrowprops=dict(arrowstyle="->", color='r'))
ax.text(600000, 20, '2008 Swing-State Counties', weight='bold')
plt.show()
df = pd.read_clipboard(sep=',')
or create a file and read with pd.read_csv
.state,county,total_votes,dem_votes,rep_votes,dem_share
PA,Erie County,127691,75775,50351,60.08
PA,Bradford County,25787,10306,15057,40.64
PA,Tioga County,17984,6390,11326,36.07
PA,McKean County,15947,6465,9224,41.21
PA,Potter County,7507,2300,5109,31.04
PA,Wayne County,22835,9892,12702,43.78
PA,Susquehanna County,19286,8381,10633,44.08
PA,Warren County,18517,8537,9685,46.85
OH,Ashtabula County,44874,25027,18949,56.94
OH,Lake County,121335,60155,59142,50.46
PA,Crawford County,38134,16780,20750,44.71
OH,Lucas County,219830,142852,73706,65.99
OH,Fulton County,21973,9900,11689,45.88
OH,Geauga County,51102,21250,29096,42.23
OH,Williams County,18397,8174,9880,45.26
PA,Wyoming County,13138,5985,6983,46.15
PA,Lackawanna County,107876,67520,39488,63.1
PA,Elk County,14271,7290,6676,52.2
PA,Forest County,2444,1038,1366,43.18
PA,Venango County,23307,9238,13718,40.24
OH,Erie County,41229,23148,17432,57.01
OH,Wood County,65022,34285,29648,53.61
PA,Cameron County,2245,879,1323,39.92
PA,Pike County,24284,11493,12518,47.87
PA,Lycoming County,49237,18381,30280,37.77
PA,Sullivan County,3120,1233,1841,40.11
OH,Lorain County,146859,85276,59068,59.1
OH,Trumbull County,106911,64145,40164,61.48
PA,Mercer County,53821,26411,26565,49.85
OH,Henry County,14840,6320,8239,43.43
PA,Clinton County,14791,7097,7504,48.61
PA,Clarion County,17766,6756,10737,38.62
PA,Luzerne County,135175,72492,61127,54.25
OH,Defiance County,19195,8399,10407,44.69
PA,Jefferson County,18802,6447,12057,34.84
OH,Portage County,78206,41856,34822,54.59
PA,Columbia County,28063,13230,14477,47.75
OH,Huron County,25582,12076,12884,48.36
OH,Medina County,90451,40924,48189,45.89
OH,Seneca County,27449,13087,13823,48.62
PA,Clearfield County,33813,14555,18662,43.82
PA,Centre County,75763,41950,32992,55.97
PA,Monroe County,68443,39453,28293,58.23
OH,Paulding County,9769,4165,5317,43.92
PA,Northumberland County,33939,14329,19018,42.97
PA,Montour County,8023,3364,4574,42.38
PA,Butler County,90425,32260,57074,36.11
PA,Armstrong County,30081,11138,18542,37.53
OH,Hancock County,36981,13870,22420,38.23
OH,Putnam County,18680,5281,13072,28.79
PA,Union County,17400,7333,9859,42.65
OH,Mahoning County,127032,79173,45319,63.57
PA,Carbon County,26923,13464,12957,50.96
PA,Lawrence County,42103,19711,21851,47.43
OH,Ashland County,25168,9300,15158,38.07
OH,Crawford County,21173,8288,12316,40.18
OH,Richland County,61122,25727,34034,43.05
OH,Wyandot County,10977,4461,6270,41.56
OH,Wayne County,52142,21712,29342,42.49
OH,Van Wert County,14652,5178,9168,36.06
OH,Stark County,187545,96990,86743,52.76
PA,Northampton County,135587,75255,58551,56.24
PA,Schuylkill County,63057,28300,33767,45.6
OH,Columbiana County,48487,21882,25585,46.07
OH,Allen County,50263,19522,29940,39.43
PA,Indiana County,37302,17065,19727,46.39
PA,Snyder County,15479,5382,9900,35.22
PA,Beaver County,84488,40499,42895,48.56
PA,Mifflin County,16502,5375,10929,32.97
OH,Hardin County,13114,5013,7749,39.26
PA,Lehigh County,152473,87089,63382,57.88
PA,Huntingdon County,18632,6621,11745,36.05
PA,Blair County,53102,19813,32708,37.72
OH,Carroll County,13953,6423,7097,47.47
OH,Mercer County,21271,5853,15100,27.92
PA,Cambria County,65670,32451,31995,50.36
OH,Morrow County,16643,6177,10067,38.01
OH,Marion County,29017,12870,15454,45.45
PA,Juniata County,9711,3068,6484,32.12
OH,Auglaize County,23486,6727,16395,29.07
PA,Westmoreland County,176873,72721,102294,41.55
PA,Berks County,180000,97047,80513,54.66
PA,Allegheny County,651436,373153,272347,57.81
OH,Holmes County,11113,3141,7720,28.94
OH,Tuscarawas County,42950,21498,20454,51.28
PA,Dauphin County,129529,69975,58238,54.58
PA,Perry County,19745,6396,13058,32.88
PA,Bucks County,332924,179031,150248,54.37
OH,Jefferson County,35939,17635,17559,50.1
OH,Knox County,28231,11014,16640,39.84
PA,Lebanon County,58297,23310,34314,40.45
OH,Logan County,22217,7936,13848,36.43
OH,Union County,24928,8761,15744,35.71
OH,Shelby County,23668,7317,15924,31.47
PA,Washington County,98047,46122,50752,47.61
OH,Coshocton County,16863,7689,8675,47.01
PA,Montgomery County,422419,253393,165552,60.49
OH,Delaware County,92416,36653,54778,40.1
OH,Harrison County,7787,3683,3872,48.76
OH,Darke County,25793,7964,17290,31.56
PA,Cumberland County,113304,48306,63739,43.11
PA,Bedford County,22443,6059,16124,27.32
PA,Lancaster County,228137,99586,126568,44.03
PA,Franklin County,63641,21169,41906,33.56
PA,Somerset County,35168,12878,21686,37.26
OH,Champaign County,18887,7385,11141,39.86
PA,Chester County,254354,137833,114421,54.64
PA,York County,194210,82839,109268,43.12
OH,Guernsey County,17325,7625,9197,45.31
OH,Miami County,52807,18372,33417,35.47
OH,Belmont County,32411,16302,15422,51.38
OH,Muskingum County,39071,17730,20549,46.33
PA,Fulton County,6306,1576,4642,25.34
PA,Fayette County,52560,25866,26081,49.79
PA,Philadelphia County,717329,595980,117221,83.56
PA,Adams County,44491,17633,26349,40.09
PA,Delaware County,297004,178870,115273,60.81
OH,Clark County,66770,31958,33634,48.73
PA,Greene County,15976,7829,7889,49.81
OH,Noble County,6172,2474,3450,41.77
OH,Fairfield County,71945,29250,41580,41.32
OH,Perry County,15404,7261,7721,48.46
OH,Montgomery County,278511,145997,128679,53.14
OH,Preble County,21002,6999,13562,34.01
OH,Monroe County,6982,3705,3066,54.74
OH,Greene County,83589,33540,48936,40.67
OH,Pickaway County,23726,9077,14228,38.96
OH,Morgan County,6608,2966,3440,46.29
OH,Fayette County,11694,4401,7102,38.25
OH,Washington County,18802,1238,17019,6.8
OH,Warren County,106216,33398,71691,31.75
OH,Ross County,31840,14455,16759,46.33
OH,Vinton County,5646,2463,3021,44.9
OH,Clermont County,95480,31611,62559,33.57
OH,Brown County,20113,7503,12192,38.1
OH,Jackson County,13993,5397,8219,39.67
OH,Meigs County,10354,4094,6015,40.47
OH,Pike County,12506,6033,6162,49.44
OH,Adams County,11388,4170,6914,37.62
OH,Gallia County,13318,4777,8247,36.71
OH,Scioto County,32571,14926,16994,46.73
OH,Lawrence County,27194,11262,15415,42.2
FL,Jackson County,21565,7671,13717,35.86
FL,Escambia County,154447,61572,91411,40.25
FL,Santa Rosa County,76185,19470,55972,25.81
FL,Okaloosa County,95529,25872,68789,27.33
FL,Holmes County,8589,1446,7033,17.06
FL,Walton County,27046,7174,19561,26.84
FL,Washington County,11131,2863,8178,25.93
FL,Nassau County,38304,10618,27403,27.93
FL,Gadsden County,22510,15582,6811,69.58
FL,Leon County,148608,91747,55705,62.23
FL,Jefferson County,7957,4088,3797,51.85
FL,Madison County,8907,4270,4544,48.44
FL,Hamilton County,5587,2364,3179,42.65
FL,Calhoun County,6244,1821,4345,29.53
FL,Liberty County,3278,895,2339,27.67
FL,Columbia County,28128,9171,18670,32.94
FL,Duval County,415761,202618,210537,49.04
FL,Baker County,11059,2327,8672,21.15
FL,Bay County,81127,23653,56683,29.45
FL,Suwannee County,17662,4916,12534,28.17
FL,Taylor County,9366,2803,6457,30.27
FL,Wakulla County,14376,5311,8877,37.43
FL,Lafayette County,3359,642,2679,19.33
FL,Saint Johns County,105844,35791,69222,34.08
FL,Gulf County,7205,2149,4980,30.15
FL,Clay County,94577,26697,67203,28.43
FL,Bradford County,11676,3430,8136,29.66
FL,Union County,5293,1300,3940,24.81
FL,Franklin County,6029,2134,3818,35.86
FL,Alachua County,125519,75565,48513,60.9
FL,Gilchrist County,7819,1996,5656,26.09
FL,Putnam County,33171,13236,19637,40.26
FL,Dixie County,7264,1925,5194,27.04
FL,Flagler County,49031,24726,23951,50.8
FL,Levy County,18725,6711,11754,36.35
FL,Marion County,162022,70839,89628,44.14
FL,Volusia County,243824,127795,113938,52.86
FL,Lake County,146926,62948,82802,43.19
FL,Citrus County,76158,31460,43706,41.85
FL,Sumter County,48868,17655,30866,36.39
FL,Seminole County,205895,99335,105070,48.6
FL,Brevard County,287859,127620,157589,44.74
FL,Orange County,462711,273009,186832,59.37
FL,Hernando County,87901,41886,45021,48.19
FL,Pasco County,214866,102417,110104,48.2
FL,Polk County,244833,113865,128878,46.91
FL,Osceola County,100670,59962,40086,59.93
FL,Pinellas County,463282,248299,210066,54.17
FL,Hillsborough County,513312,272963,236355,53.59
FL,Indian River County,70591,29710,40176,42.52
FL,Highlands County,44783,18135,26221,40.89
FL,Hardee County,7412,2568,4763,35.03
FL,Manatee County,151994,70034,80721,46.46
FL,Okeechobee County,12786,5108,7561,40.32
FL,Saint Lucie County,120579,67125,52512,56.11
FL,Sarasota County,207353,102686,102897,49.95
FL,DeSoto County,10131,4383,5632,43.76
FL,Martin County,78294,33508,44143,43.15
FL,Glades County,3358,1381,1938,41.61
FL,Charlotte County,85158,39031,45205,46.34
FL,Palm Beach County,590500,361271,226037,61.51
FL,Hendry County,10879,4998,5780,46.37
FL,Lee County,269276,119701,147608,44.78
FL,Collier County,141988,54450,86379,38.66
FL,Broward County,733899,492640,237729,67.45
FL,Miami-Dade County,863486,499831,360551,58.09
FL,Monroe County,40272,20907,18933,52.48
OH,Ottawa County,23069,12049,10618,53.16
OH,Sandusky County,30373,15601,14190,52.4
OH,Summit County,269059,155105,110499,58.36
OH,Athens County,31098,20722,9742,68.02
OH,Butler County,173777,66030,105341,38.53
OH,Clinton County,19305,6558,12409,34.58
OH,Cuyahoga County,665352,458422,199880,69.64
OH,Franklin County,560325,334709,218486,60.5
OH,Hamilton County,425086,225213,195530,53.53
OH,Highland County,19186,6856,11907,36.54
OH,Hocking County,12961,6259,6364,49.58
OH,Licking County,82356,33932,46918,41.97
OH,Madison County,17454,6532,10606,38.11
Upvotes: 63