[英]Seaborn lineplot - connecting dots of scatterplot
I have problem with sns lineplot and scatterplot.我对 sns 线图和散点图有疑问。 Basically what I'm trying to do is to connect dots of a scatterplot to present closest line joining mapped points.
基本上我要做的是连接散点图的点以呈现连接映射点的最近线。 Somehow lineplot is changing width when facing points with tha same x axis values.
当面对具有相同 x 轴值的点时,线图会以某种方式改变宽度。 I want to lineplot to be same, solid line all the way.
我希望线图始终相同,实线。
The code:编码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
data = {'X': [13, 13, 13, 12, 11], 'Y':[14, 11, 13, 15, 20], 'NumberOfPlanets':[2, 5, 2, 1, 2]}
cts = pd.DataFrame(data=data)
plt.figure(figsize=(10,10))
sns.scatterplot(data=cts, x='X', y='Y', size='NumberOfPlanets', sizes=(50,500), legend=False)
sns.lineplot(data=cts, x='X', y='Y',estimator='max', color='red')
plt.show()
The outcome:结果:
Any ideas?有任何想法吗?
EDIT:编辑:
If I try using pyplot it doesn't work either: Code:如果我尝试使用 pyplot 它也不起作用:代码:
plt.plot(cts['X'], cts['Y'])
Outcome:结果:
I need one line, which connects closest points (basically what is presented on image one but with same solid line).我需要一条线,它连接最近的点(基本上是在图像一中呈现的,但具有相同的实线)。
Ok, I have finally figured it out.好的,我终于想通了。 The reason lineplot was so messy is because data was not properly sorted.
线图如此混乱的原因是数据没有正确排序。 When I sorted dataframe data by 'Y' values, the outcome was satisfactory.
当我按“Y”值对 dataframe 数据进行排序时,结果令人满意。
data = {'X': [13, 13, 13, 12, 11], 'Y':[14, 11, 13, 15, 20], 'NumberOfPlanets':[2, 5, 2, 1, 2]}
cts = pd.DataFrame(data=data)
cts = cts.sort_values('Y')
plt.figure(figsize=(10,10))
plt.scatter(cts['X'], cts['Y'], zorder=1)
plt.plot(cts['X'], cts['Y'], zorder=2)
plt.show()
Now it works.现在它起作用了。 Tested it also on other similar scatter points.
还在其他类似的散点上对其进行了测试。 Everything is fine:) Thanks!
一切都很好:) 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.