简体   繁体   中英

How do you combine the two Seaborn line plot figures while keeping the range of y values?

I want to combine two figures with different ranges like figure

图一

This figure was synthesized by drawing two figures. However, I would like to use a seaborn library at once. but now it shows like

图二 .

How could I combine different y range figures by using seaborn or any other python plot library?

I add my python code, too

import seaborn as sns
import numpy as np
with sns.axes_style('white'):
     ax = sns.lineplot(x='unit_step', y='accuracy', hue='data_type', style='data_type', markers=True, dashes=True, data=data)
     ax.set_xticks(np.arange(1,6))

As I understand your question as a request for how to plot two datasets of very different ranges into one subplot, here a little hint how you could achieve that.

tl;dr: in the end you just need to put plt.twinx() before the plt.plot()-command which shall have a secondary y-axis.

First assume two functions, which have very different value ranges in the same definition range, f(x) = x³ and f(x) = sin(x) :

x = np.linspace(-2*np.pi, 2*np.pi, 100)
y1 = x**3
y2 = np.sin(x)

This can be plotted like this:

fig, axs= plt.subplots(1, 2, figsize=(12, 6))

axs[0].plot(x, y1, 'b', x, y2, 'r')
axs[0].set_xlabel('X')
axs[0].set_ylabel('Y')

axs[1].plot(x, y1, 'b')
axs[1].set_xlabel('X')
axs[1].set_ylabel('Y1')

axs_sec = axs[1].twinx()        # <--- create secondary y-axis
axs_sec.plot(x, y2, 'r')
axs_sec.set_ylabel('Y2')

在此处输入图片说明

On the left hand side, you can see that the sine is nearly invisible if plotted into the same y-axis. On the right hand side the sine is plotted into a secondary y-axis, so that it gets its own scaling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM