5

I am very new to plotly. I need to add specific X axis-labels for the box-plot. The X- axis names i need to put is shown in an array x_label. Any suggestions will be appreciated.

import plotly
plotly.tools.set_credentials_file(username='demo', api_key='u759djdol')
import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py
import plotly.tools as tls


#x = np.array([1,2,3,4,5,6,7,8,9])

atn_time_1_1 = np.array([22,31,71,74,17,22,23,51,39])
atn_time_1_2 = np.array([27,30,20,40,29,22,36,35,38,32])
atn_time_1_3 = np.array([34,15,14,86,22,17,27,33,26,22,102,14])
atn_time_1_4 = np.array([26,31,85,23,24,42,74,33,43,45,12,26,38])
atn_time_1_9 = np.array([65,37,22,47,23,56,59,65,64,50,42,19,40,22,39])
atn_time_1_16 = np.array([15,36,18,58,27,89,36,24,15,29,21,21,60,36,126,54])
atn_time_4_12 = np.array([18,14,56,110,32,73,17,23,32,25,44,24])
atn_time_4_16 =np.array([32,17,36,80,32,28,70,34,36,42,29,58,26,18,20])
atn_time_4_36 = np.array([12,23,86,30,45,63,30,43,64,39,46,49,19,34])

x_label = ['1 vs 1','1 vs 2','1 vs 3','1 vs 4''1 vs 9','1 vs 16'
         ,'4 vs 12','4 vs 16','4 vs 36']

data = [atn_time_1_1,atn_time_1_2,atn_time_1_3,atn_time_1_4,
    atn_time_1_9,atn_time_1_16,atn_time_4_12,atn_time_4_16,
    atn_time_4_36]

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

ax.boxplot(data)
ax.set_xlabel('run_tasks')
ax.set_ylabel('Colony emigration time (min)')

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plot_url = py.plot(plotly_fig, 'simple_boxplot')

2 Answers 2

13

Plotly is a little bit different from matplotlib, to add axis labels you need to create a trace object and edit the name keyword.

import numpy as np
import plotly.graph_objs as go
import plotly.plotly as py import plotly plotly.tools.set_credentials_file(username='demo', api_key='u759djdol')

atn_time_1_1 = np.array([22, 31, 71, 74, 17, 22, 23, 51, 39]) 
atn_time_1_2 = np.array([27, 30, 20, 40, 29, 22, 36, 35, 38, 32]) atn_time_1_3 = np.array([34, 15, 14, 86, 22, 17, 27, 33, 26, 22, 102, 14]) 
atn_time_1_4 = np.array([26, 31, 85, 23, 24, 42, 74, 33, 43, 45, 12, 26, 38]) 
atn_time_1_9 = np.array([65, 37, 22, 47, 23, 56, 59, 65, 64, 50, 42, 19, 40, 22, 39]) 
atn_time_1_16 = np.array([15, 36, 18, 58, 27, 89, 36, 24, 15, 29, 21, 21, 60, 36, 126, 54]) 
atn_time_4_12 = np.array([18, 14, 56, 110, 32, 73, 17, 23, 32, 25, 44, 24]) 
atn_time_4_16 = np.array([32, 17, 36, 80, 32, 28, 70, 34, 36, 42, 29, 58, 26, 18, 20]) 
atn_time_4_36 = np.array([12, 23, 86, 30, 45, 63, 30, 43, 64, 39, 46, 49, 19, 34])

x_label = ['1 vs 1', '1 vs 2', '1 vs 3', '1 vs 4','1 vs 9',
           '1 vs 16', '4 vs 12', '4 vs 16', '4 vs 36']

d = [atn_time_1_1, atn_time_1_2, atn_time_1_3, atn_time_1_4,
     atn_time_1_9, atn_time_1_16, atn_time_4_12, atn_time_4_16,
     atn_time_4_36]

data = []
#loop through data to create plotly trace 
for i in range(len(d)):
    trace = go.Box(
        y=d[i],
        name=x_label[i], #add labels
    )
    data.append(trace)

#style layout 
layout = go.Layout(
    title="Title",
    xaxis=dict(
        title="X Label"
    ),
    yaxis=dict(
        title="Y label"
    ) ) 
fig=go.Figure(layout=layout,data=data) 
py.iplot(fig)

This code will give you the outputenter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

That's my bad, the for-loop got commented out when I was formatting it on here. You got an index error, because the code was trying to index with i, yet there was no i initialized. I fixed the code above.
1

Just an update: ImportError: The plotly.plotly module is deprecated, please install the chart-studio package and use the chart_studio.plotly module instead.

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.