在bokeh中使用for循环绘制多个图表

2024-03-28 17:25:13 发布

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

我需要画几张水平条形图。我应用了下面的for循环来绘制它们,但是我得到了一个错误

chart_cols = 'respondent_age respondent_gender respondent_edu respondent_occupation Religion Caste_cat CM_choice Likely_winner'.split()
for f in chart_cols:
    count = df[f].value_counts()

    p = figure(plot_height=400, plot_width=400, title='Chart',toolbar_location=None)

    p.title.align = "right"
    p.xaxis.axis_label = 'Number of respondents'
    p.yaxis.axis_label = 'Something'

    p.hbar(y=sorted(df[f].unique()), height=0.7, left=0,
      right=count, color=Category20,
       alpha=0.7)

    show(p)
    print('Done')

错误

^{pr2}$

如何使用for循环绘制多个图表?文档中给出的示例显示了使用row函数对2个绘图所做的相同操作。在


Tags: rightdfforplottitlecount错误chart
1条回答
网友
1楼 · 发布于 2024-03-28 17:25:13

下面的代码现在可以工作了。 对于图表列中的f: count=df[f].value\u counts()

p = figure(plot_height=400, plot_width=400, title='Chart',toolbar_location=None)

p.title.align = "right"
p.xaxis.axis_label = 'Number of respondents'
p.yaxis.axis_label = str(f)

p.hbar(y=sorted(df[f].unique()), height=0.7, left=0,
      right=count, color=Category20[len(df[f].unique())], alpha = 0.7)

show(p)

相关问题 更多 >