我正致力于为英雄联盟创造一些关于玩家游戏的分析。我尝试使用plotly创建直方图,日期范围在x轴上,没有。你的游戏。这有效,但我不能每个月都能获得单独的酒吧。我尝试过使用xaxis,' size'对象,但这不会改变任何东西,我猜因为x轴是日期形式。
所以问题,在Plotly中,如何将直方图上条形的大小从每月的bin大小更改为每日bin大小?
以下是代码示例:
from datetime import date, timedelta
import random
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
from plotly import tools
from plotly.offline import *#plotly.offline.iplot()
init_notebook_mode(connected=True)
############## create date ranges #################
d1 = date(2014, 3, 22) # start date
d2 = date(2014, 6, 22) # end date
delta = d2 - d1 # timedelta
dates = []
for i in range(delta.days + 1):
dates.append((d1 + timedelta(days=i)))
#################################################
def games_p_day():
sizeo = 1
trace_total = go.Histogram(
y=[random.randint(1, 10) for y in range(1, 100)],
x=dates,
name = 'total games',
xbins=dict(
size=sizeo
)
)
trace_wins = go.Histogram(
y=[random.randint(1, 10) for y in range(1, 100)],
x=dates,
name = 'won games',
xbins=dict(
size=sizeo
)
)
trace_losses = go.Histogram(
y=[random.randint(1, 10) for y in range(1, 100)],
x=dates,
name = 'lost games',
xbins=dict(
size=sizeo
)
)
layout = dict(
title = "Wins and losses over time",
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label='1m',
step='month',
stepmode='backward'),
dict(count=6,
label='6m',
step='month',
stepmode='backward'),
dict(step='all')
])
),
rangeslider=dict(),
type='date',
),
bargap=0.2,
bargroupgap=0.1)
data=[trace_total]
fig = go.Figure(data=data, layout=layout)
iplot(fig, filename = "Wins and losses over time")
games_p_day()
任何帮助都非常感激。 哦,如果你看到其他任何可以帮助我的东西(即糟糕的代码结构),请告诉我!
答案 0 :(得分:1)
直方图是数字数据的representation of the distribution。在我看来,您打算在此发布的是每天到每周的数据汇总。也就是说,只要您希望有一个时间维度,而不希望在x轴上进行计数,平均值或任何其他聚合函数。如果是这种情况,那么您面临挑战的关键就不在于图本身,而在于诸如resample('W-Mon', on='index').sum()
之类的聚合和时间函数。以下是一些示例:
采样的原始数据图:
原始数据代码:
import pandas as pd
import numpy as np
import datetime
# data
np.random.seed(12)
numdays=100
dates = pd.date_range('1/1/2020', periods=numdays)
games = np.random.randint(low=100, high=200, size=numdays).tolist()
losses = np.random.randint(low=0, high=100, size=numdays).tolist()
wins = list(np.array(games)-np.array(wins))
df = pd.DataFrame({'games': games,
'wins':wins,
'losses':losses}, index=dates)
# resample daily data to weekly sums
df2=df.reset_index().resample('W-Mon', on='index').sum()
df2['formatted_date'] = pd.to_datetime(df3.index)
df2['year'] = df2.formatted_date.apply(lambda x: x.year)
df2['week_of_year'] = df2.formatted_date.apply(lambda x: x.weekofyear)
df2['year_week'] = df2['year'].map(str)+'_'+df3['week_of_year'].map(str)
# build and show plotly plot for daily games
fig = go.Figure(data=[go.Bar(name='games', x=df.index, y=df['games'])])
fig.show()
绘制每周汇总的数据。日期作为索引:
每周汇总数据的代码。日期作为索引:
# build and show plotly plot for weekly games. Dates as index
fig = go.Figure(data=[go.Bar(name='games', x=df2.index, y=df2['games'])])
fig.show()
绘制每周汇总的数据。年和周编号作为索引:
每周汇总数据的代码。年和周编号作为索引:
# build and show plotly plot for weekly games. Year and week number as index
fig = go.Figure(data=[go.Bar(name='games', x=df2['year_week'], y=df2['games'])])
fig.show()
绘制每周汇总数据的图表,按获胜和亏损进行划分:
每周汇总数据的代码,按获胜和失败分配:
import pandas as pd
import numpy as np
import datetime
# data
np.random.seed(12)
numdays=100
dates = pd.date_range('1/1/2020', periods=numdays)
games = np.random.randint(low=100, high=200, size=numdays).tolist()
losses = np.random.randint(low=0, high=100, size=numdays).tolist()
wins = list(np.array(games)-np.array(wins))
df = pd.DataFrame({'games': games,
'wins':wins,
'losses':losses}, index=dates)
# resample daily data to weekly sums
df2=df.reset_index().resample('W-Mon', on='index').sum()
df2['formatted_date'] = pd.to_datetime(df3.index)
df2['year'] = df2.formatted_date.apply(lambda x: x.year)
df2['week_of_year'] = df2.formatted_date.apply(lambda x: x.weekofyear)
df2['year_week'] = df2['year'].map(str)+'_'+df3['week_of_year'].map(str)
fig = go.Figure(data=[go.Bar(name='victory', x=df2['year_week'], y=df2['wins']),
go.Bar(name='defeat', x=df2['year_week'], y=df2['losses'])])
fig.update_layout(barmode='group')
fig.show()