为要显示在条形上方的值添加标签
Add the label for the value to display above the bars
我创建了一个条形图并想将计数值放在每个条形图上方。
# Import the libraries
import pandas as pd
from matplotlib import pyplot as plt
# Create the DataFrame
df = pd.DataFrame({
'city_code':[1200013, 1200104, 1200138, 1200179, 1200203],
'index':['good', 'bad', 'good', 'good', 'bad']
})
# Plot the graph
df['index'].value_counts().plot(kind='bar', color='darkcyan',
figsize=[15,10])
plt.xticks(rotation=0, horizontalalignment="center", fontsize=14)
plt.ylabel("cities", fontsize=16)
我得到以下结果
我想在每个栏的顶部添加值。我从value_counts得到的count的值。
像这样:
感谢所有帮助过的人。
您可以使用ax.text
逐个添加标签,使用for循环。
但是 matplotlib 中已经内置了 method 来执行此操作。
您可以将示例中的行 df['index'].value_counts().plot(kind='bar', color='darkcyan', figsize=[15,10])
更改为
d = df['index'].value_counts()
p = ax.bar(d.index, d.values,color='darkcyan')
ax.bar_label(p)
完整的例子是:
fig, ax = plt.subplots(figsize=(4, 3))
# Create the DataFrame
df = pd.DataFrame({
'city_code':[1200013, 1200104, 1200138, 1200179, 1200203],
'index':['good', 'bad', 'good', 'good', 'bad']
})
# Plot the graph
d = df['index'].value_counts()
p = ax.bar(d.index, d.values,color='darkcyan')
ax.bar_label(p)
plt.xticks(rotation=0, horizontalalignment="center", fontsize=14)
plt.ylabel("cities", fontsize=16)
fig.show()
输出图像如下所示:
使用 patches
和 annotate
的示例:
# Import the libraries
import pandas as pd
from matplotlib import pyplot as plt
# Create the DataFrame
df = pd.DataFrame(
{
"city_code": [1200013, 1200104, 1200138, 1200179, 1200203],
"index": ["good", "bad", "good", "good", "bad"],
}
)
# Plot the graph
ax = df["index"].value_counts().plot(kind="bar", color="darkcyan", figsize=[15, 10])
plt.xticks(rotation=0, horizontalalignment="center", fontsize=14)
plt.ylabel("cities", fontsize=16)
for p in ax.patches:
ax.annotate(
str(p.get_height()), xy=(p.get_x() + 0.25, p.get_height() + 0.1), fontsize=20
)
plt.savefig("test.png")
结果:
我创建了一个条形图并想将计数值放在每个条形图上方。
# Import the libraries
import pandas as pd
from matplotlib import pyplot as plt
# Create the DataFrame
df = pd.DataFrame({
'city_code':[1200013, 1200104, 1200138, 1200179, 1200203],
'index':['good', 'bad', 'good', 'good', 'bad']
})
# Plot the graph
df['index'].value_counts().plot(kind='bar', color='darkcyan',
figsize=[15,10])
plt.xticks(rotation=0, horizontalalignment="center", fontsize=14)
plt.ylabel("cities", fontsize=16)
我得到以下结果
我想在每个栏的顶部添加值。我从value_counts得到的count的值。 像这样:
感谢所有帮助过的人。
您可以使用ax.text
逐个添加标签,使用for循环。
但是 matplotlib 中已经内置了 method 来执行此操作。
您可以将示例中的行 df['index'].value_counts().plot(kind='bar', color='darkcyan', figsize=[15,10])
更改为
d = df['index'].value_counts()
p = ax.bar(d.index, d.values,color='darkcyan')
ax.bar_label(p)
完整的例子是:
fig, ax = plt.subplots(figsize=(4, 3))
# Create the DataFrame
df = pd.DataFrame({
'city_code':[1200013, 1200104, 1200138, 1200179, 1200203],
'index':['good', 'bad', 'good', 'good', 'bad']
})
# Plot the graph
d = df['index'].value_counts()
p = ax.bar(d.index, d.values,color='darkcyan')
ax.bar_label(p)
plt.xticks(rotation=0, horizontalalignment="center", fontsize=14)
plt.ylabel("cities", fontsize=16)
fig.show()
输出图像如下所示:
使用 patches
和 annotate
的示例:
# Import the libraries
import pandas as pd
from matplotlib import pyplot as plt
# Create the DataFrame
df = pd.DataFrame(
{
"city_code": [1200013, 1200104, 1200138, 1200179, 1200203],
"index": ["good", "bad", "good", "good", "bad"],
}
)
# Plot the graph
ax = df["index"].value_counts().plot(kind="bar", color="darkcyan", figsize=[15, 10])
plt.xticks(rotation=0, horizontalalignment="center", fontsize=14)
plt.ylabel("cities", fontsize=16)
for p in ax.patches:
ax.annotate(
str(p.get_height()), xy=(p.get_x() + 0.25, p.get_height() + 0.1), fontsize=20
)
plt.savefig("test.png")
结果: