我有以下时间序列数据:
Datum Menge
1/1/2018 0:00 19.5
1/1/2018 0:15 19.0
1/1/2018 0:30 19.5
1/1/2018 0:45 19.5
1/1/2018 1:00 21.0
1/1/2018 1:15 19.5
1/1/2018 1:30 20.0
1/1/2018 1:45 23.0
,数据框data
的形状为(14880,2)。在Menge
列中,只有11807个值可用,其余为nan
我试图按以下方式绘制它:
data.plot()
plt.show()
这给了我
但是我想使用seaborn
或plotly
对于seaborn
,我已经尝试过:
x = data.Datum
y = data.Menge.values
sns.lineplot(x = x, y = y, data = data)
它给我的输出为:
Out[3]: <matplotlib.axes._subplots.AxesSubplot at 0x21286bb8668>
随即打开一个新的图形窗口,但显示为Figure 1 (Not Responding)
所以,我有两个问题:
Datum
值。如何更改?答案 0 :(得分:2)
考虑玩具数据框:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Datum": ['1/1/2018 0:00',
'1/1/2018 0:15',
'1/1/2018 0:30',
'1/1/2018 0:45',
'1/1/2018 1:00',
'1/1/2018 1:15',
'1/1/2018 1:30',
'1/1/2018 1:45 '],
"Menge": [19.5, 19.,19.5,19.5,21,19.5,20,23]})
sns.lineplot(x="Datum", y="Menge", data=df)
plt.xticks(rotation=15)
plt.title('seaborn-matplotlib example')
plt.show()
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
trace1 = go.Scatter(x=df.Datum,
y=df.Menge,
name = "plotly example",
line = dict(color = 'blue'),
opacity = 0.4)
layout = dict(title='plotly example',)
fig = dict(data=[trace1], layout=layout)
iplot(fig)
答案 1 :(得分:1)
即使对于多个时间序列,最干净的设置是:
密谋:iplot()
季节性:lineplot()
密谋:
iplot([{
'x': df.index,
'y': df[col],
'name': col
} for col in df.columns])
Seaborn:
sns.lineplot(data = df)
完整的seaborn和plot代码:
以下代码示例将使您可以在离线Jupyter Notebook中生成两个图。
# imports
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from IPython.core.display import display, HTML
import matplotlib as mpl
import cufflinks as cf
import seaborn as sns
import pandas as pd
import numpy as np
# setup
display(HTML("<style>.container { width:35% !important; } .widget-select > select {background-color: gainsboro;}</style>"))
init_notebook_mode(connected=True)
np.random.seed(1)
mpl.rcParams['figure.dpi']= 440
# sample data from cufflinks
df = cf.datagen.lines()
# plotly
iplot([{
'x': df.index,
'y': df[col],
'name': col
} for col in df.columns])
# seaborn
sns.set_style("darkgrid")
plt.xticks(rotation=45)
sns.lineplot(data = df)
答案 2 :(得分:0)
最后给出的Plotly解决方案很棒。使用图形对象,也很容易为我的数据设置标签字典-因为政府提供的数据名称几乎没有描述性。所以只有几行代码:
columns = ['CPALTT01USM657N', 'UNRATE', 'TB3MS']
names = {'CPALTT01USM657N': 'Inflation Rate',
'UNRATE': 'Unemployment Rate',
'TB3MS': '3 Mo. T-Bill Rate'}
fig = go.Figure([{
'x': monthly_data['DATE'],
'y': monthly_data[col],
'name': names[col]
} for col in columns])
fig.show(renderer='iframe')
答案 3 :(得分:0)
现在比以前在Plotly中容易得多。
import untangle
xsd_file = "C:\\code\\python\\xsd\\test.xsd"
obj = untangle.parse(xsd_file)
res = obj.xs_schema.xs_element.xs_complexType.xs_sequence.xs_element.xs_complexType.xs_sequence.xs_element.xs_simpleType.xs_restriction.xs_enumeration
# IMPORTS
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
# EXTRACT THE DATA
df = pd.DataFrame(
{
"Datum": [
"1/1/2018 0:00",
"1/1/2018 0:15",
"1/1/2018 0:30",
"1/1/2018 0:45",
"1/1/2018 1:00",
"1/1/2018 1:15",
"1/1/2018 1:30",
"1/1/2018 1:45 ",
],
"Menge": [19.5, 19.0, 19.5, 19.5, 21, 19.5, 20, 23],
}
)
(代码与最上面的答案中的代码相同)
px.line(x="Datum", y="Menge", data_frame=df, title="plotly example")