Search code examples
pythonpandasdataframevincent

Create a multiline graph using vincent from a Pandas DataFrame


I got a issue similar to the following one : Creating a multiline graph using Vincent, but I did'nt find the solution.

I want to display a multiline graph to get the number of discussions processed by users, on a chat tool, for each hour of the day.
I got a DataFrame with the following format below. The left column represents the hours, and each other column the number of discussions by user for each hour :

      user1  user2
...    ...    ...
8       0     0
9       1     0
10      0     0
11      0     0
12      11    0
...    ...   ...

Note : I didn't display all the DF but I got rows from 0 to 23.

I tried the following code to build my graph :

graph = vincent.Line(df)
graph.axis_titles(x='Hour', y='Number of discussions')
graph.legend(title="Users")

Which gave me the following error :

array is not broadcastable to correct shape

I did't really get what's wrong, I tried something else. I copied my index as a serie in my DF, what gave me the following format :

    user1  user2    hour
...    ...    ...   ...
8       0     0     8
9       1     0     9
10      0     0     10
11      0     0     11
12      11    0     12
...    ...   ...    ...

And I tried the following code :

graph = vincent.Line(df, iter_idx='hour')
graph.axis_titles(x='Hour', y='Number of discussions')
graph.legend(title="Users")

Here, I got no error, but like davidheller in the post I linked, I got a graph without any line, and just the value 0 in absicssa. I guess my parameters for the vincent.Line object are not correct, but I don't see how to pass them. Do you have any idea ?


Solution

  • I got the same problem, what I winded up doing was creating a dictionary instead of the data frame.

    user_d = {}
    for col in list(df.columns):
        user_d[col] = list(df[col])
    
    user_d['index'] = df.index.tolist()
    
    graph = vincent.Line(user_d, iter_idx='hour')
    graph.axis_titles(x='Hour', y='Number of discussions')
    graph.legend(title="Users")
    

    Hope this helps!