我试图制作一个带有双y轴的有序条形图。到目前为止,我已经制作了图表,但我正在尝试将其从最多注册到最少注册。
以下是我对数据框进行排序的代码:
#Merging the Two DFs
viz_df = pd.merge(totenrl_df, gpaAvg_df, on = 'Institution')
#Sorting Values based off Enrollment
viz_df = viz_df.sort_values('Total Female Enrollment', ascending = False)
#Reducing DF to exclude those who do not have enrollment numbers
viz_df = viz_df[viz_df['Total Female Enrollment'] != 0]
viz_df.head(10)
这让我产生了: 我遇到的问题是让Bokeh理解这个有序图。这是我生成图表的代码:
#Creating Output File
output_notebook()
#Setting Dimensions
p = figure(plot_width = 800, plot_height = 400)
#Creating Point Renderer
p.cross(viz_df.index,viz_df['Totals, Female: Cumulative GPA (Tot. F)'].values,line_width = 0.5, color = 'red', y_range_name = 'GPA')
#Creating alterante range for GPA
p.extra_y_ranges = {'GPA': Range1d(start = 0, end = 4.0)}
p.add_layout(LinearAxis(y_range_name = 'GPA'), 'right')
#Creating Bar Values
h = viz_df['Total Female Enrollment'].values
#Adding Bar Renderer
p.vbar(x = viz_df.index, bottom = 0, top = h, width = .5)
show(p)
我不知道我做错了什么,我认为应该有效。谢谢你的帮助。
答案 0 :(得分:0)
我刚刚遇到了同样的问题。问题是,Bokeh 是指索引列来提供数据。当你排序时,Pandas 不会自动重置索引(你可以在你共享的表的索引列中观察到这一点),所以你所要做的就是,在排序之后,放入 .reset_index() 并且它应该运行良好。