首页 > 解决方案 > Overlay median values line graph on a barchart of counts by category in python

问题描述

The following code gives me bar charts that give a count per category. I would like to overlay the median value of a numeric feature per unique category.

What would I need to add to the following code?

def plot_bars(xy_merge, cols):
    for col in cols:
        fig = plt.figure(figsize=(6,6)) # define plot area
        ax = fig.gca() # define axis  

        counts = xy_merge[col].value_counts() # find the counts for each unique category
        counts.plot.bar(ax = ax, color = 'blue') # Use the plot.bar method on the counts data frame

        ax.set_title('Counties and median log rental per categorical variable: ' + col) # Give the plot a main title
        ax.set_xlabel(col) # Set text for the x axis
        ax.set_ylabel('Count')# Set text for y axis

        plt.show()

plot_cols = ['state', 'rucc', 'urban_influence']
plot_bars(xy_merge, plot_cols) 

data frame

bar chart

标签: python-3.xpandasmatplotlib

解决方案


推荐阅读