please notice barplot('Age',False) runs without any error but fails to plot graph in chrome我正在使用spyder分析来自kaggle的ibm hr员工数据集的员工流失情况。对于绘制图形,我使用的是plotly库,但无法使用该库绘制条形图,而我可以成功绘制散点图和饼图。
附上我的工作的示例代码
spyder成功地在Chrome中打印了plot_distribution('Age', False)
此图形,但无法绘制barplot('Age', False)
def barplot(var_select, x_no_numeric) :
tmp1 = data[(data['Attrition'] != 0)]
tmp2 = data[(data['Attrition'] == 0)]
tmp3 = pd.DataFrame(pd.crosstab(data[var_select], data['Attrition']), )
tmp3['Attr%'] = tmp3[1] / (tmp3[1] + tmp3[0]) * 100
if x_no_numeric == True:
tmp3 = tmp3.sort_values(1, ascending=False)
color = ['skyblue', 'gold']
trace1 = go.Bar(
x = tmp1[var_select].value_counts().keys().tolist(),
y = tmp2[var_select].value_counts().values.tolist(),
name = 'yes_attrition', opacity = 0.8,
marker = dict(color = 'gold', line=dict(color='#000000',width=1)))
trace2 = go.Bar(
x = tmp1[var_select].value_counts().keys().tolist(),
y = tmp2[var_select].value_counts().values.tolist(),
name = 'no_attrition', opacity = 0.8,
marker = dict(color='skyblue', line=dict(color='#000000',width=1)))
trace3 = go.Scatter(
x = tmp3.index,
y = tmp3['Attr%'],
yaxis = 'y2',
name = '%Attrition', opacity= 0.5,
marker= dict(color='black',line=dict(color='#000000',width=0.5)))
layout = dict(title= str(var_select),
xaxis = dict(),
yaxis = dict(title='count'),
yaxis2 = dict(range= [-0,75],
overlaying = 'y',
anchor = 'x',
side = 'right',
Zeroline = False,
showgrid = False,
title = '%Attrition'))
fig = go.Figure(data=[trace1, trace2, trace3], layout= layout)
py.plot(fig)
plot_distribution('Age', False)
barplot('Age', False)