我想使用 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
最佳答案
seaborn 轴级绘图,方法相同。
seaborn 图形级图,您必须遍历每个轴,但未显示。.text 或 .annotate 指定
matplotlib.pyplot.text
weight 或fontweight 参数。matplotlib.pyplot.annotate ,它使用与 .text 相同的 kwargs。plt.rc('text', usetex=True),weight='bold' 可能没有效果。在这种情况下,使用 LaTex 语法 \textbf{}(例如 plt.text(600000, 20, r'\textbf{2008 Swing-State Counties}')) python 3.10、pandas 1.4.2、matplotlib 3.5.1、seaborn 0.11.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([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'))
.annotate 无粗体plt.annotate('12 largest counties; most vote for Obama', xy=(650000, 52),
xytext=(400000, 35), fontsize=10, arrowprops=dict(arrowstyle="->", color='b'))
.annotate 与 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 与 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([x for x in range(0, 1000000, 100000)], rotation=40)
ax.set_yticks([x for x in 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([x for x in range(0, 1000000, 100000)], rotation=40)
ax.set_yticks([x for x in 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=',') 读取或创建一个文件并使用 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
关于python - 如何在绘图中添加粗体注释文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162414/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以