如何在python Matplotlib中使交互式绘图

时间:2019-11-28 04:21:16

标签: python pandas matplotlib

数据如下:

    Id  Timestamp               Data    Group
0   1   2013-08-12 10:29:19.673 40.0    1
1   2   2013-08-13 10:29:20.687 50.0    2
2   3   2013-09-14 10:29:20.687 40.0    3
3   4   2013-10-14 10:29:20.687 30.0    4
4   5   2013-11-15 10:29:20.687 50.0    5
                    ...

我能够绘制法线图,但想使用Matplotlib创建交互式图。我使用了代码:

%matplotlib notebook
%matplotlib inline

df['Timestamp'] = pd.to_datetime(df['Timestamp'])   
df1 = df[df['Group'] ==1]
plt.plot( x = 'Timestamp', y = 'Data',figsize=(20, 10))
plt.show()

它返回了一个空图和错误

  

TypeError:剧情得到了意外的关键字参数“ x”

怎么了?

更新:
完成错误

TypeError                                 Traceback (most recent call last)
<ipython-input-33-0eb3ff7c9c6c> in <module>()
      9 df1 = df[df['Group'] ==1]
     10 # df1 = df.groupby(df['Group'])
---> 11 plt.plot( x = df1['Timestamp'], y = df1['Data'], figsize=(20, 10))

2 frames
/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py in __call__(self, *args, **kwargs)
    169             if pos_only in kwargs:
    170                 raise TypeError("{} got an unexpected keyword argument {!r}"
--> 171                                 .format(self.command, pos_only))
    172 
    173         if not args:

TypeError: plot got an unexpected keyword argument 'x'

2 个答案:

答案 0 :(得分:1)

编辑:它是错误消息的解决方案,而不是说明如何创建需要event handling(文档:Interactive plot)的交互式绘图


要使用x=,您必须使用df.plot()而不是plt.plot()

df.plot(x='Timestamp', y='Data', figsize=(20, 10))

如果要使用plt.plot(),则必须设置不包含x=的值

plt.plot(df['Timestamp'], df['Data'])

因为它作为位置参数(*args)而不是命名参数而获得。

它没有参数figsize=

请参阅文档matplotlib.pyplot.plot()

中的参数

答案 1 :(得分:0)

根据@GIRISHkuniyal的建议,使用plotly.express可以使绘图与代码交互:

import plotly.express as px
fig = px.line(df1, 'Timestamp', 'Data')
fig.show()

谢谢

相关问题