barplot标签单独的酒吧与他们的高度matplotlib python

时间:2017-07-01 13:34:29

标签: python pandas matplotlib

我有一个条形图,但是想在每个条形图上方放置一个标签,对应每个年龄组的净销售额,以使每个条形图的高度对读者来说更明显吗?

代码:

cd.loc[(cd.birth_year >= 1996) & (cd.birth_year <= 1998), "Age"] = "21 & Under"
cd.loc[(cd.birth_year >= 1983) & (cd.birth_year <= 1995), "Age"] = "22 to 34"
cd.loc[(cd.birth_year >= 1973) & (cd.birth_year <= 1982), "Age"] = "35 to 44"
cd.loc[(cd.birth_year >= 1963) & (cd.birth_year <= 1972), "Age"] = "45 to 54"
cd.loc[(cd.birth_year >= 1953) & (cd.birth_year <= 1962), "Age"] = "55 to 64"
cd.loc[(cd.birth_year >= 1900) & (cd.birth_year <= 1952), "Age"] = "65 & Over"
agedata = cd.groupby("Age").agg({"LTIME_NO_ORDERS": sum})

barplot = agedata.plot(kind="bar", colormap="Set3", legend=None)

# title and axis labels
plt.title("Net Sales of Age Groups", weight="bold", fontsize=20)
plt.ylabel("Net Sales (£)", fontsize=16)
plt.xlabel("Age", fontsize=16)
plt.xticks(rotation="horizontal")

rects = barplot.patches
labels = ["label1", "label2", "label3", "label4", "label5", "label6"]

for rect, label in zip(rects, labels):
    height = rect.get_height()
    barplot.text(rect.get_x() + rect.get_width()/2, height+5, label, ha="center", va="bottom")

plt.show()

当前情节:  enter image description here

感谢您的帮助!

更新:如何使标签自动对应年龄范围总和而不是手动输入标签名称?

1 个答案:

答案 0 :(得分:0)

@JsonAutoDetect(
        fieldVisibility = Visibility.ANY, 
        getterVisibility = Visibility.NONE, 
        setterVisibility = Visibility.NONE)
class Statistics {

    @JsonProperty ("Shots on Goal")
    Stats ShotsonGoal;

    @JsonProperty ("Shots off Goal")
    Stats ShotsoffGoal;

    @JsonProperty ("Total Shots")
    Stats TotalShots;

    @JsonProperty ("Blocked Shots")
    Stats BlockedShots;

    @JsonProperty ("Shots insidebox")
    Stats Shotsinsidebox;

    @JsonProperty ("Shots outsidebox")
    Stats Shotsoutsidebox;
    Stats Fouls;

    @JsonProperty ("Corner Kicks")
    Stats CornerKicks;
    Stats Offsides;

    @JsonProperty ("Ball Possession")
    StatsPercent BallPossesion;

    @JsonProperty ("Yellow Cards")
    Stats YellowCards;

    @JsonProperty ("Red Cards")
    Stats RedCards;

    @JsonProperty ("Goalkeeper Saves")
    Stats GoalkeeperSaves;

    @JsonProperty ("Total passes")
    Stats Totalpasses;

    @JsonProperty ("Passes accurate")
    Stats Passesaccurate;

    @JsonProperty ("Passes %")
    StatsPercent Passes;

// ... getters/setters
}