我想知道Python是否可以绘制带有多个变量的时间序列的数据可用性。下面显示一个取自Visavail.js - A Time Data Availability Chart的示例。

最佳答案

这是在Jupyter Notebook中使用plotly的建议:

python - Python中的数据可用性图表-LMLPHP

码:

import random
import pandas as pd
import plotly.express as px
from random import choices

# random data with a somewhat higher
# probability of 1 than 0 to mimic OPs data
random.seed(1)
vals=[0,1]
prob=[0.4, 0.6]
choices(vals, prob)

data=[]
for i in range(0,5):
    data.append([choices(vals, prob)[0] for c in range(0,10)])

# organize data in a pandas dataframe
df=pd.DataFrame(data).T
df.columns=['Balance Sheet', 'Closing Price', 'Weekly Report', 'Analyst Data', 'Annual Report']
drng=pd.date_range(pd.datetime(2080, 1, 1).strftime('%Y-%m-%d'), periods=df.shape[0]).tolist()
df['date']=[d.strftime('%Y-%m-%d') for d in drng]
dfm=pd.melt(df, id_vars=['date'], value_vars=df.columns[:-1])

# plotly express
fig = px.bar(dfm, x="date", y="variable", color='value', orientation='h',
             hover_data=["date"],
             height=600,
             color_continuous_scale=['firebrick', '#2ca02c'],
             title='Data Availabiltiy Plot',
             template='plotly_white',
            )

fig.update_layout(yaxis=dict(title=''), xaxis=dict(title='', showgrid=False, gridcolor='grey',
                  tickvals=[],
                            )
                 )
fig.show()

10-01 15:05