在 Y 轴上或 Python 中每个条形的顶部显示值
Display Values either on Y-axis or on top of each bar in Python
这是我的代码输出:
plt.figure(figsize = (7,4))
df1.groupby(["Area"])["Crop_Value(hg/ha)"].sum().sort_values(ascending = False).nlargest(5).plot(kind = "bar")
plt.title("Top 5 Countries with most crop production")
plt.show()
我正在尝试可视化农作物产量最高的前 5 个国家/地区。 Y 轴中的值不像它们那样有用。
如何将其更改为实际作物产值?或者
如何在每个条的顶部显示值?
谢谢
您需要使用不同于标准的格式化程序 ScalarFormatter
, e.g. a FormatStrFormatter
:
import matplotlib.pyplot as plt
plt.subplots()
plt.bar(['India', 'Brazil'], [3e8,2e8])
plt.gca().yaxis.set_major_formatter(plt.matplotlib.ticker.FormatStrFormatter('%.1e'))
这是我的代码输出:
plt.figure(figsize = (7,4))
df1.groupby(["Area"])["Crop_Value(hg/ha)"].sum().sort_values(ascending = False).nlargest(5).plot(kind = "bar")
plt.title("Top 5 Countries with most crop production")
plt.show()
我正在尝试可视化农作物产量最高的前 5 个国家/地区。 Y 轴中的值不像它们那样有用。
如何将其更改为实际作物产值?或者
如何在每个条的顶部显示值?
谢谢
您需要使用不同于标准的格式化程序 ScalarFormatter
, e.g. a FormatStrFormatter
:
import matplotlib.pyplot as plt
plt.subplots()
plt.bar(['India', 'Brazil'], [3e8,2e8])
plt.gca().yaxis.set_major_formatter(plt.matplotlib.ticker.FormatStrFormatter('%.1e'))