如何使用 pd.DataFrame 在条形图顶部显示最大值或值?
How to show max or value on bar chart top with pd.DataFrame?
我想在条形图的顶部显示条形图的值。我为此使用 DataFrame。
这是我的代码:
import matplotlib.pyplot as plt
import pandas as pd
from decimal import Decimal
data = {
'L': [86.0, 91.7, 92.3, 92.8],
'D': [87.6, 90.5, 91.1, 91.1],
'K': [87.0, 91.5, 92.5, 92.0],
'N': [84.0, 84.9, 91.7, 89.9],
'S': [91.0, 87.0, 88.8, 91.0]
}
barWidth = 0.9
colours = {"L": "#837E7C", "D": "#98F99F", "K": "#FFE24A", "N": "#E8ADAA", "S": "#87C0FF" }
df = pd.DataFrame(data,columns=['L','D', 'K', 'N', 'S'], index=['a', 'b', 'c', 'd'],)
df.plot.barh(figsize=(4,8), color=colours, fontsize = 14)
plt.ylabel('Type', fontsize = 14)
plt.yticks(rotation=90, ha="center", rotation_mode='anchor')
plt.label('Result', fontsize = 14)
plt.show()
结果是:
[![enter image description here][1]][1]
但没有在顶部显示值。如何使用我的代码。
下面是你想要的吗?您需要一种解决方法来迭代多条形图。第一步是定义一个 ax
对象。第二步是使用 for
循环获取每个值
酒吧.
import matplotlib.pyplot as plt
import pandas as pd
from decimal import Decimal
data = {
'L': [86.0, 91.7, 92.3, 92.8],
'D': [87.6, 90.5, 91.1, 91.1],
'K': [87.0, 91.5, 92.5, 92.0],
'N': [84.0, 84.9, 91.7, 89.9],
'S': [91.0, 87.0, 88.8, 91.0]
}
barWidth = 0.9
colours = {"L": "#837E7C", "D": "#98F99F", "K": "#FFE24A", "N": "#E8ADAA", "S": "#87C0FF" }
df = pd.DataFrame(data,columns=['L','D', 'K', 'N', 'S'], index=['a', 'b', 'c', 'd'])
ax = df.plot.barh(figsize=(4,8), color=colours, fontsize = 14)
for p in ax.patches:
ax.annotate(str(p.get_width()), (p.get_x() + p.get_width(), p.get_y()), xytext=(1, 2), textcoords='offset points')
plt.ylabel('Type', fontsize = 14)
plt.yticks(rotation=90, ha="center", rotation_mode='anchor')
plt.show()
我想在条形图的顶部显示条形图的值。我为此使用 DataFrame。 这是我的代码:
import matplotlib.pyplot as plt
import pandas as pd
from decimal import Decimal
data = {
'L': [86.0, 91.7, 92.3, 92.8],
'D': [87.6, 90.5, 91.1, 91.1],
'K': [87.0, 91.5, 92.5, 92.0],
'N': [84.0, 84.9, 91.7, 89.9],
'S': [91.0, 87.0, 88.8, 91.0]
}
barWidth = 0.9
colours = {"L": "#837E7C", "D": "#98F99F", "K": "#FFE24A", "N": "#E8ADAA", "S": "#87C0FF" }
df = pd.DataFrame(data,columns=['L','D', 'K', 'N', 'S'], index=['a', 'b', 'c', 'd'],)
df.plot.barh(figsize=(4,8), color=colours, fontsize = 14)
plt.ylabel('Type', fontsize = 14)
plt.yticks(rotation=90, ha="center", rotation_mode='anchor')
plt.label('Result', fontsize = 14)
plt.show()
结果是:
[![enter image description here][1]][1]
但没有在顶部显示值。如何使用我的代码。
下面是你想要的吗?您需要一种解决方法来迭代多条形图。第一步是定义一个 ax
对象。第二步是使用 for
循环获取每个值
酒吧.
import matplotlib.pyplot as plt
import pandas as pd
from decimal import Decimal
data = {
'L': [86.0, 91.7, 92.3, 92.8],
'D': [87.6, 90.5, 91.1, 91.1],
'K': [87.0, 91.5, 92.5, 92.0],
'N': [84.0, 84.9, 91.7, 89.9],
'S': [91.0, 87.0, 88.8, 91.0]
}
barWidth = 0.9
colours = {"L": "#837E7C", "D": "#98F99F", "K": "#FFE24A", "N": "#E8ADAA", "S": "#87C0FF" }
df = pd.DataFrame(data,columns=['L','D', 'K', 'N', 'S'], index=['a', 'b', 'c', 'd'])
ax = df.plot.barh(figsize=(4,8), color=colours, fontsize = 14)
for p in ax.patches:
ax.annotate(str(p.get_width()), (p.get_x() + p.get_width(), p.get_y()), xytext=(1, 2), textcoords='offset points')
plt.ylabel('Type', fontsize = 14)
plt.yticks(rotation=90, ha="center", rotation_mode='anchor')
plt.show()