Reputation: 11
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))
Upvotes: 1
Views: 4259
Reputation: 10860
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.
Upvotes: 3