2

I am enjoying using plotly and wanted to plot boxplots for my data.

From their website, I do the following:

import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

y0 = np.random.randn(50)
y1 = np.random.randn(50)+1

trace0 = go.Box(
    y=y0,
    name = 'Sample A',
    marker = dict(
        color = 'rgb(214, 12, 140)',
    )
)
trace1 = go.Box(
    y=y1,
    name = 'Sample B',
    marker = dict(
        color = 'rgb(0, 128, 128)',
    )
)
data = [trace0, trace1]
py.iplot(data)

The challenge that I have is that I do not know the total number of "trace" is unknown. For example:

titanic = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.csv")

I would like to plot a boxplot, by column 'embarked', a boxplot of the 'fare' column. Since the total number of unique values in 'embarked' is unknown, I do not want to hardcode that in.

Does anyone know how I can do this properly in plotly?

Thank you!

1 Answer 1

2

You could loop over your unique values in embarked and add a trace for each one. In this case there is also nan which needs separate treatment.

for embarked in titanic.embarked.unique():

enter image description here

import plotly
plotly.offline.init_notebook_mode()
import pandas as pd
import numpy as np

titanic = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.csv")

traces = list()
for embarked in titanic.embarked.unique():
    if str(embarked) == 'nan':
       traces.append(plotly.graph_objs.Box(y=titanic[pd.isnull(titanic.embarked)].fare,
                                            name = str(embarked)
                                            )
                     )
    else:
        traces.append(plotly.graph_objs.Box(y=titanic[titanic.embarked == embarked].fare,
                                            name = embarked
                                            )
                     )
plotly.offline.iplot(traces)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I am not seeing anything being displayed as it comes back blank. FYI I am in jupyter notebook.
Weird. I was also using a Jupyter Notebook and just tried it with a fresh notebook.
Why do the offline thing? I read the documentation, but it really only focuses on what happens and it why it's better. Does it take up less ram?
I think on the client side it does not matter, I just usually don't need to upload my plots or share them, that's why I use offline.
OK got it so it doesn't load anything online and I don't have to deal with the privacy settings.

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.