绘制直方图matplotlib,在x轴上标记而不是计数

时间:2018-02-15 02:28:37

标签: python pandas matplotlib

我希望在python中使用value_counts()或等效的直方图绘制直方图。我的数据如下:

Lannister                      8
Stark                          8
Greyjoy                        7
Baratheon                      6
Frey                           2
Bolton                         2
Bracken                        1
Brave Companions               1
Darry                          1
Brotherhood without Banners    1
Free folk                      1
Name: attacker_1, dtype: int64

您可以使用任何可重现的代码,例如:

pd.DataFrame({'Family':['Lannister', 'Stark'], 'Battles':[6, 8]})

我正在使用

plt.hist(battles.attacker_1.value_counts())

histogram

我希望x轴显示姓氏,而不是战斗的数量,我希望战斗的数量是直方图。我尝试使用一系列姓氏(Lannister重复8次)而不是使用value_counts(),并认为这可能有效,但我不确定如何做到这一点。

3 个答案:

答案 0 :(得分:3)

想出来。

battles.attacker_1.value_counts().plot(kind = 'bar')

答案 1 :(得分:2)

对于 vanilla matplotlib 解决方案,请将public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888 ConfigureAuth(app); } public void ConfigureAuth(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ApplicationCookie); app.UseCookieAuthentication( new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, // application cookie which is generic for all the authentication types. LoginPath = new PathString("/login.aspx"), // redirect if not authenticated. AuthenticationMode = AuthenticationMode.Passive }); app.UseWsFederationAuthentication( new WsFederationAuthenticationOptions { AuthenticationType = "test auth", MetadataAddress = "https://adfs-server/federationmetadata/2007-06/federationmetadata.xml", //adfs meta data. Wtrealm = "https://localhost/", //reltying party Wreply = "/home.aspx"//redirect }); AuthenticateAllRequests(app, "test auth"); } private static void AuthenticateAllRequests(IAppBuilder app, params string[] authenticationTypes) { app.Use((context, continuation) => { if (context.Authentication.User != null && context.Authentication.User.Identity != null && context.Authentication.User.Identity.IsAuthenticated) { return continuation(); } else { context.Authentication.Challenge(authenticationTypes); return Task.Delay(0); } }); } xticklabels一起使用:

xticks

哪个收益率:

histogram plot

答案 2 :(得分:1)

您可以查看pandas plot

df.set_index('Family').Battles.plot(kind='bar')

enter image description here