Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line plot over bar plot using Seaborn - Line plot won't render

I am trying to plot a line plot over a bar plot and cannot get the line plot to render with the bar chart. I can render the bar chart all the time and the line only when commenting out ax2. When I do render the line plot the dates show up in integer instead of date format. I think it has something to do with the X axis but cannot figure it out.

fig, ax = plt.subplots(figsize = (10, 10))
ax = sns.lineplot(x='Submission Date', y='Rating', data=df_cd)
ax2 = ax.twinx()
ax2 = sns.barplot(x='Submission Date', y='Count Handled', data=df_cd)
ax.set_xticklabels(ax.get_xticks())
plt.show()

enter image description here

enter image description here

like image 989
dgresh24 Avatar asked Sep 03 '26 02:09

dgresh24


1 Answers

The command plt.subplots(figsize = (10, 10)), indicates that you want to divide canvas and create the subplots on it. For your current requirement, you could do something like -

ax = sns.barplot(x='Submission Date', y='Count Handled', data=df_cd) ax2 = ax.twinx() ax2.plot(ax.get_xticks(), df_cd.Rating)

Here, you are creating the barplot first and adding the line plot over it with the same x-axis.

like image 104
SaRa Avatar answered Sep 05 '26 18:09

SaRa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!