python的X轴标题

2024-03-29 10:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我对策划很陌生。我需要为方框图添加特定的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')

Tags: importtimeasnpfigpltplotlyax
1条回答
网友
1楼 · 发布于 2024-03-29 10:20:50

Plotlymatplotlib稍有不同,要添加轴标签,您需要创建一个跟踪对象并编辑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)

这段代码将给出输出enter image description here

相关问题 更多 >