1

I want to plot a graph using jupyter noterbook + plotly, based on data that I have grouped by day and ID.

The DataFrame looks like this:

DataFrame

I have tried this in matplotlib but I couldn't figure it out either.

What I want is a graph, with x = hour column and y shows the counts. For example:

Hour 0: x[0] = [2622, 48, 374, 210, 305, 1427, 83, 12]

Hour 1: x[1] = [2920, 25, 357, 140, 283, 79, 14, 53]

... 

with x = [0, 1, 2, ..., 23]

That means each x have many y-values. How can I plot this in plotly? Do I have to do a dimension reduction? If yes, how?

Thanks in advance for help!

0

2 Answers 2

3

This suggestion will not match your data structure 100%. But we'll handle that later if this turns out to be what you're looking for.


You can achieve what you want by building a plotly figure with multiple traces. If you'd like your datapoints to be a bit jittered to avoid overlapping, you can use go.Box() to get this:

Plot:

enter image description here

Complete code:

This is set up to work in an off-line Jupyter Notebook. Plotly figures are created directly in the notebook.

# imports
import plotly
from plotly import tools
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go

# setup
init_notebook_mode(connected=True)
np.random.seed(123)

# dataframe
df = pd.DataFrame({'x0':[2622, 48, 374, 210, 305, 1427, 83, 12],
                    'x1':[2920, 25, 357, 140, 283, 79, 14, 53]})

# build traces for each x
traces = {}
for col in df.columns:
    traces['trace_' + col] = go.Box(name = col, y=df[col],
                                    boxpoints = 'all',
                                    pointpos = 0,
                                    marker = dict(color = 'rgb(84, 173, 39)'),
                                    line = dict(color = 'rgba(0,0,0,0)'),
                                    fillcolor = 'rgba(0,0,0,0)')
# convert data to form required by plotly
data = list(traces.values())

# build figure
fig = go.Figure(data, layout)

# plot figure
iplot(fig)
Sign up to request clarification or add additional context in comments.

Comments

2
  • For a given count fix the abscissa
  • count is a 1D array of length 8
  • So create 8 identical abscissae and mark their data points
  • Here it's just about point no need to use plot

Below I just use the two x value you gave, you can use a for loop for the remaining

import numpy as np
import matplotlib.pyplot as plt

y1 = [2622, 48, 374, 210, 305, 1427, 83, 12]
x1 = 0*np.ones(8)

y2 = [2920, 25, 357, 140, 283, 79, 14, 53]
x2 = 1*np.ones(8)

plt.scatter(x1, y1)
plt.scatter(x2, y2)
plt.show()

The corresponding loop code is below

import numpy as np
import matplotlib.pyplot as plt


count = [[2622, 48, 374, 210, 305, 1427, 83, 12], [2920, 25, 357, 140, 283, 79, 14, 53]]
hour = np.ones((8, ) ,dtype = int)

for i in range(2):
  plt.scatter(i*hour, count[i])

plt.show()

Graph

enter image description here

For the entire dataframe just replace

  • range(2) by range(24)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.