[英]Matplotlib print values on bars in subplots
Using the above code, I have created 5 five subplots:使用上面的代码,我创建了 5 个五个子图:
values = {"x_values" : ["ENN", "CNN", "ENN-CNN"],
"eu" : [11, 79.97, 91],
"man" : [11, 80, 90],
"min3" : [11, 79.70, 90],
"min4" : [11, 79.50, 90],
"che" : [12, 78, 89]}
df = pd.DataFrame(data=values)
fig, axs = plt.subplots(2, 3, figsize=(10,6))
eu = axs[0, 0].bar(df["x_values"], df["eu"]
man = axs[0, 1].bar(df["x_values"], df["man"])
min3 = axs[0, 2].bar(df["x_values"], df["min3"])
min4 = axs[1, 0].bar(df["x_values"], df["min4"])
che = axs[1, 1].bar(df["x_values"], df["che"])
fig.delaxes(axs[1, 2])
They print as they should, but I also want to add to the bars the y value of every bar.他们按应有的方式打印,但我还想将每个条形图的 y 值添加到条形图。 Just like in the picture enter image description here
就像图片中一样在这里输入图片描述
I have tried the code below, but it doesn't print anything, no error but also no print我已经尝试了下面的代码,但它没有打印任何东西,没有错误但也没有打印
for index, value in enumerate(df["corresponding_df"]):
plt.text(value, index, str(value))
If I try variable-name.text(value, index, str(value))
I get error 'BarContainer' object has no attribute 'text'
.如果我尝试
variable-name.text(value, index, str(value))
我得到错误'BarContainer' object has no attribute 'text'
。 If fig.text
again not print.如果
fig.text
再次不打印。 If axs[subplot-index].text
I can only see a number at the end of the window outside the plots.如果
axs[subplot-index].text
我只能在地块外的 window 末尾看到一个数字。 Any suggestion?有什么建议吗?
Try this using bar_label in matplotlib 3.4.0+:在 matplotlib 3.4.0+ 中使用 bar_label 试试这个:
values = {"x_values" : ["ENN", "CNN", "ENN-CNN"],
"eu" : [11, 79.97, 91],
"man" : [11, 80, 90],
"min3" : [11, 79.70, 90],
"min4" : [11, 79.50, 90],
"che" : [12, 78, 89]}
df = pd.DataFrame(data=values)
fig, axs = plt.subplots(2, 3, figsize=(10,6))
eu = axs[0, 0].bar(df["x_values"], df["eu"])
axs[0,0].bar_label(eu)
man = axs[0, 1].bar(df["x_values"], df["man"])
axs[0,1].bar_label(man)
min3 = axs[0, 2].bar(df["x_values"], df["min3"])
axs[0,2].bar_label(min3)
min4 = axs[1, 0].bar(df["x_values"], df["min4"])
axs[1,0].bar_label(min4)
che = axs[1, 1].bar(df["x_values"], df["che"])
axs[1,1].bar_label(che)
fig.delaxes(axs[1, 2])
Output: Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.