简体   繁体   English

我正在尝试 plot 三行 matplotlib 但它显示为一个

[英]I am trying to plot three lines with matplotlib but it is showing up as one

I have a DF That I am using to plot how many userlogs a player has had in a given hour.我有一个 DF,我正在使用 plot 玩家在给定小时内拥有多少用户日志。 I want to see what time of the day they are most active.我想看看他们一天中什么时候最活跃。

    username    hour    freq
0   Player 1    0        74
1   Player 1    1        37
2   Player 1    2        6
3   Player 2    3        152
4   Player 2    5        90
5   Player 2    6        57
6   Player 3    7        219
7   Player 3    8        443
8   Player 3    9        557

I then use that DF and try and plot it like this:然后我使用那个 DF 并尝试 plot 像这样:

plt.rcParams['figure.figsize'] = 10,5
for name in playername: 
    playertimes = new_df[new_df.username.isin([name])]
    plt.plot(playertimes['freq'], c='Black', ls='--', marker = 's', ms=7, label = name)
plt.legend(loc='upper left', bbox_to_anchor=(1,1))
plt.xticks(list(range(0,24)), new_df['hour'], rotation = 'vertical')
plt.show()

My logic is that I am going to use the playertimes df as a temporary df so I can plot only the freq of each player.我的逻辑是我将使用 playertimes df 作为临时 df,所以我可以 plot 仅每个玩家的频率。

I was hoping for one line for each player but what I got was this:我希望每个玩家都有一条线,但我得到的是:

Failed Graph失败的图表

I tried messing with a few things but can not seem to get it to work.我试着弄乱了一些东西,但似乎无法让它发挥作用。 Any ideas?有任何想法吗?

The list of users has to be unique to go through the dataframe just once, and the dataframe should be sorted by 'hour' to display the results as expected.用户列表必须是 go 到 dataframe 的唯一一次,并且 dataframe 应该按预期按“小时”排序以显示结果。 In order to display the proper values on the x axis, you just need to actually plot those values on the x-axis.为了在 x 轴上显示正确的值,您只需要在 x 轴上实际 plot 那些值。 The code below includes all these changes:下面的代码包括所有这些更改:

plt.rcParams['figure.figsize'] = 10,5
linestyles = ['-', '--', ':']
markers = ['s', 'o', 'v']
for i, name in enumerate(df.username.unique()): 
    playertimes = df[df.username.isin([name])].sort_values('hour')
    plt.plot(playertimes['hour'], playertimes['freq'], c='Black', 
ls=linestyles[i], marker =markers[i], ms=7, label = name)
plt.legend(loc='upper left', bbox_to_anchor=(1,1))
plt.xticks(range(0,24), rotation = 'vertical')
plt.show()

3 个地块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM