0

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

docproc85
  • 1
  • 2
  • can you provide data? It would be better to provide dummy data for your xy_merge or dataframe – Dishin H Goyani Dec 09 '19 at 15:33
  • Hi, have added screenshots of the df and the 'rucc' bar charts produced. I would like to overlay a line graph of median rent per unique 'state', 'rucc', 'urban_influence' – docproc85 Dec 09 '19 at 18:33
  • this is not exact answer but you can use ax.plot() for that. check relative ans. [Multiple func. on same figure](https://stackoverflow.com/questions/22276066/how-to-plot-multiple-functions-on-the-same-figure-in-matplotlib) and [plot+bar chart](https://stackoverflow.com/questions/38810009/matplotlib-plot-bar-and-line-charts-together) – Dishin H Goyani Dec 10 '19 at 04:17

0 Answers0