3

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

dgresh24
  • 77
  • 1
  • 9
  • 1
    You cannot mix a barplot and a lineplot from the same data in seaborn. You could however map all the real dates to the same numbers as used in the bar. So plot the data against the numbers 0,1,2,3,.... etc – ImportanceOfBeingErnest Dec 16 '19 at 18:26

1 Answers1

4

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.

SaRa
  • 316
  • 1
  • 4