0

Current Output Expected Output Goal: I want to see the countplot and line plot on the same graph

Problem: i am only seeing the countplot presented

Treaty Year,As of Combined Incurred Amount
0,1999,69512.0
1,2001,552564.63
2,2001,1955896.62
3,2004,976349.81
4,2013,787644.0
5,2013,26937.63
6,1999,95510.0
7,1999,1670073.53
8,1999,1752561.23
9,1999,222182.81
%matplotlib inline

import matplotlib
import seaborn as sns

matplotlib.rc_file_defaults()

ax1 = sns.set_style(style=None, rc=None )

fig, ax1= plt.subplots(figsize=(12,3))

ax1 = sns.lineplot(x='Year', y='Val', data=df, marker='o', sort = True, ax=ax1)
ax2 = ax1.twinx()

sns.countplot(x='Year', data=df, ax=ax2)

ax1.get_yaxis().set_major_formatter(ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
fig.show()
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Jul 14 '22 at 17:00
  • The ticks on the axis of a line plot are indexed based on the values passed to `x`, however, a count plot is a bar plot, and the ticks are always indexed from 0, with the values passed to `x` being used as labels. – Trenton McKinney Jul 14 '22 at 17:02
  • editing right now, thank you! – Luke Bernstein Jul 14 '22 at 17:05
  • plot the barplot first (with x='year'), and then plot the line plot. for `x` in the line plot used the dataframe index (0, 1, 2, ...., etc.). – Trenton McKinney Jul 14 '22 at 17:29

0 Answers0