简体   繁体   中英

Dual Plotting X-Axis via Seaborn

EDIT: I have reworded the question as it wasn't as clear as it should have been. I have 2 datasets (df3 and df4 which respectively hold information for total head and efficiency) with a common independent variable (flow rate). I am looking to plot both of them in the same graph but the dependent variables have different Y-axes. I initially used lmplot for the polynomial order functionality but this was unsuccessful in having both plots appear in one window. I would like assistance with combining both my scatter plot and regression plots into one plot which shows the overlap between the datasets.

I have used the following approach to generate my charts:

ax2.scatter(df3['Flow_Rate_(KG/S)'], df2['Efficiency_%'], color='pink')
ax2.scatter(df4['Flow_Rate_(KG/S)'], df4['Total Head'], color='teal')
plt.show()

The reason why it is important for the lines to be plotted against each other is that to monitor pump performance, we need to have both the total head (M) and efficiency % of the pump to understand the relationship and subsequent degradation of performance.

The only other way I could think of is to write the polynomial functions as equations to be put into arguments in the plot function and have them drawn out as such. I haven't yet tried this but thought I'd ask if there are any other alternatives before I head down this pathway.

Thank you for your time.

散点图

SOLUTION: For those interested, I used the .twinx() libraries with regplot as below.

fig, ax = plt.subplots()
ax2 = ax.twinx() #This allows the common axes (flow rate) to be shared
sbn.regplot(x="Flow_Rate_(KG/S)", y="Total Head", data=df3, order=2, ax=ax)
sbn.regplot(x="Flow_Rate_(KG/S)", y="Efficiency_%", data=df4, order=2, 
ax=ax2)
ax2.set_ylim(0,1)#This is used to set the limit for efficiency. Without this being set, the curves do not line up.

Let me try to rephrase the problem: You have two datasets with common independent values, but different dependent values (f(x), g(x) respectively). You want to plot them both in the same graph, however the dependent values have totally different ranges. Therefore you want to have two different y axes, one for each dataset. The data should be plotted as a scatter plot and a regression line should be shown for each of them; you are more interested in seeing the regression line than knowing or calculating the regression curve itself. Hence you tried to use seaborn lmplot , but you were unsuccessful to get both datasets into the same graph.

In case the above is the problem you want to solve, the answer could be the following.

lmplot essentially plots a regplot to an axes grid. Because you don't need that axes grid here, using a regplot may make more sense. You may then create an axes and a twin axes and plot one regplot to each of them.

import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df1 = pd.DataFrame({"x": np.sort(np.random.rand(30)),
                    "f": np.sort(np.random.rayleigh(size=30))})
df2 = pd.DataFrame({"x": np.sort(np.random.rand(30)),
                    "g": 500-0.1*np.sort(np.random.rayleigh(20,size=30))**2})

fig, ax = plt.subplots()
ax2 = ax.twinx()
sns.regplot(x="x", y="f", data=df1, order=2, ax=ax)
sns.regplot(x="x", y="g", data=df2, order=2, ax=ax2)


ax2.legend(handles=[a.lines[0] for a in [ax,ax2]], 
           labels=["f", "g"])
plt.show()

在此处输入图片说明

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