简体   繁体   中英

X- axis titles in plotly for python

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')

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 output在此处输入图片说明

只是更新:ImportError:不推荐使用 plotly.plotly 模块,请安装 chart-studio 包并改用 chart_studio.plotly 模块。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM