简体   繁体   English

通过Seaborn双重绘制X轴

[英]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). 我有2个数据集(df3和df4分别具有总水头和效率的信息),并具有共同的独立变量(流速)。 I am looking to plot both of them in the same graph but the dependent variables have different Y-axes. 我希望将它们都绘制在同一张图中,但是因变量具有不同的Y轴。 I initially used lmplot for the polynomial order functionality but this was unsuccessful in having both plots appear in one window. 最初,我使用lmplot来实现多项式阶数功能,但这未能成功将两个图都显示在一个窗口中。 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. 之所以要相互画线很重要,是因为要监视泵的性能,我们需要同时掌握泵的总扬程(M)和效率%,以了解性能之间的关系以及其后的性能下降。

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. 解决方案:对于感兴趣的人,我将.twinx()库与regplot一起使用,如下所示。

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). 让我尝试重述该问题:您有两个数据集,它们具有共同的独立值,但具有不同的依存值(分别为f(x),g(x))。 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. 因此,您希望有两个不同的y轴,每个数据集一个。 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. 因此,您尝试使用seaborn lmplot ,但无法将两个数据集都放入同一图中。

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. lmplot本质regplot重新绘制图绘制到轴网格上。 Because you don't need that axes grid here, using a regplot may make more sense. 因为这里不需要轴网格,所以使用regplot可能更有意义。 You may then create an axes and a twin axes and plot one regplot to each of them. 然后,您可以创建一个轴和一个双轴,并为每个轴绘制一个regplot。

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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