0

I am trying to plot both a barplot and a lineplot on the same chart using seaborn. I have seen other answers to this and apparently I need to use twinx (1 and 2) but it is definitively not working for me.

What I would like to achieve is a clean chart that looks like this:

desired

My dataset is the following:

close_date  trades  close_profit_abs close_date
close_date                                     
2021-09-29       1         43.552593 2021-09-29
2021-09-30       0          0.000000 2021-09-30
2021-10-01       0          0.000000 2021-10-01
2021-10-02       0          0.000000 2021-10-02
2021-10-03       0          0.000000 2021-10-03
2021-10-04       0          0.000000 2021-10-04
2021-10-05       2         82.135690 2021-10-05
2021-10-06       1         46.504284 2021-10-06
2021-10-07       0          0.000000 2021-10-07
2021-10-08       0          0.000000 2021-10-08
2021-10-09       0          0.000000 2021-10-09
2021-10-10       0          0.000000 2021-10-10
2021-10-11       2         80.746129 2021-10-11
2021-10-12       2         82.925197 2021-10-12
2021-10-13       1        121.899388 2021-10-13
2021-10-14       0          0.000000 2021-10-14
2021-10-15       3        181.676698 2021-10-15
2021-10-16       0          0.000000 2021-10-16
2021-10-17       2         83.343893 2021-10-17

My code is the following:

def plot1(trades_df):
    fig, ax1 = plt.subplots()

    sns.set_color_codes("pastel")
    sns.lineplot(x='close_date',
                 y='close_profit_abs',
                 data=trades_df,
                 marker='o',
                 label='Label A',
                 color='r',
                 ax=ax1)

    ax2 = ax1.twinx()
    sns.barplot(x='close_date',
                y='trades',
                data=trades_df,
                label='Label B',
                color="b",
                ax=ax2)

    sns.despine(left=True, bottom=True)
    plt.xticks(rotation=90)
    plt.ylabel("Y label", fontsize=12)
    plt.xlabel("X label", fontsize=12)
    fig.tight_layout()
    plt.show()
    return(fig)

Plotting individually seems to work. First subplot:

subplot 1

and the second plot (some adjustments are needed, like the x axis dates which are not shown like the first subplot, not sure why).

subplot 2

However when I try to combine both... I get this

broken chart

Everything is broken here, many grids, x axis dates scrambled, y axis of the axis 2 is showing as float instead of int, and most importantly, the first subplot is not even showing up.

How can I get both of the plots on the same chart? To get a result similar to the desired shown chart? Thank you!

Albert Vonpupp
  • 4,557
  • 1
  • 17
  • 20
  • The bar plot creates a categorical x-axis. To combine, you can also use a categorical x-axis for the line plot. See e.g. [Line plot over bar plot using Seaborn - Line plot won't render](https://stackoverflow.com/questions/59361990/line-plot-over-bar-plot-using-seaborn-line-plot-wont-render) – JohanC Oct 20 '21 at 12:38
  • thank you @JohanC. That helped. Now I need to adjust the scales so they match on both axis. I will post that as a separated question. Plotting is always complicated stuff to me. I am not that fluent with matplotlib. – Albert Vonpupp Oct 20 '21 at 14:14

0 Answers0