我是个新人。我需要为箱形图添加特定的X轴标签。我需要放置的X轴名称显示在数组x_label
中。任何建议将不胜感激。
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')
答案 0 :(得分:1)
Plotly
与matplotlib
略有不同,添加创建跟踪对象所需的轴标签并修改name
关键字。
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)
答案 1 :(得分:0)
只需更新:ImportError: plotly.plotly模块已弃用, 请安装chart-studio软件包并使用 而是使用chart_studio.plotly模块。