python中两个不同数据帧的散点打印数据
我有两个不同的数据帧,格式如下python中两个不同数据帧的散点打印数据,python,pandas,matplotlib,dataframe,plot,Python,Pandas,Matplotlib,Dataframe,Plot,我有两个不同的数据帧,格式如下 dfclean Out[1]: obj 0 682 1 101 2 33 dfmalicious Out[2]: obj 0 17 1 43 2 8 3 9 4 211 我的用例是绘制一个散点图,清楚地显示两个数据帧的obj值。为此,我正
dfclean
Out[1]:
obj
0 682
1 101
2 33
dfmalicious
Out[2]:
obj
0 17
1 43
2 8
3 9
4 211
我的用例是绘制一个散点图,清楚地显示两个数据帧的obj
值。为此,我正在使用python。我看了几个例子,其中使用了两列相同的数据框来绘制数据,但无法为我的用例复制数据。非常感谢您的帮助
若要在单个轴上绘制多个列组,请重复指定目标的方法
ax
选项1]
In [2391]: ax = dfclean.reset_index().plot(kind='scatter', x='index', y='obj',
color='Red', label='G1')
In [2392]: dfmalicious.reset_index().plot(kind='scatter', x='index', y='obj',
color='Blue', label='G2', ax=ax)
Out[2392]: <matplotlib.axes._subplots.AxesSubplot at 0x2284e7b8>
In [2399]: dff = dfmalicious.merge(dfclean, right_index=True, left_index=True,
how='outer').reset_index()
In [2406]: dff
Out[2406]:
index obj_x obj_y
0 0 17 682.0
1 1 43 101.0
2 2 8 33.0
3 3 9 NaN
4 4 211 NaN
In [2400]: ax = dff.plot(kind='scatter', x='index', y='obj_x', color='Red', label='G1')
In [2401]: dff.plot(kind='scatter', x='index', y='obj_y', color='Blue', label='G2', ax=ax)
Out[2401]: <matplotlib.axes._subplots.AxesSubplot at 0x11dbe1d0>
[2391]中的:ax=dfclean.reset_index().plot(kind='scatter',x='index',y='obj',
颜色class='红色',标签class='G1')
在[2392]中:df.reset_index().plot(kind='scatter',x='index',y='obj',
颜色='蓝色',标签='G2',ax=ax)
出[2392]:
选项2]
In [2391]: ax = dfclean.reset_index().plot(kind='scatter', x='index', y='obj',
color='Red', label='G1')
In [2392]: dfmalicious.reset_index().plot(kind='scatter', x='index', y='obj',
color='Blue', label='G2', ax=ax)
Out[2392]: <matplotlib.axes._subplots.AxesSubplot at 0x2284e7b8>
In [2399]: dff = dfmalicious.merge(dfclean, right_index=True, left_index=True,
how='outer').reset_index()
In [2406]: dff
Out[2406]:
index obj_x obj_y
0 0 17 682.0
1 1 43 101.0
2 2 8 33.0
3 3 9 NaN
4 4 211 NaN
In [2400]: ax = dff.plot(kind='scatter', x='index', y='obj_x', color='Red', label='G1')
In [2401]: dff.plot(kind='scatter', x='index', y='obj_y', color='Blue', label='G2', ax=ax)
Out[2401]: <matplotlib.axes._subplots.AxesSubplot at 0x11dbe1d0>
[2399]中的:dff=df恶意.merge(dfclean,right\u index=True,left\u index=True,
how='outer').reset_index()
In[2406]:dff
Out[2406]:
索引对象x对象y
0 0 17 682.0
1 1 43 101.0
2 2 8 33.0
339南
44211南
在[2400]中:ax=dff.plot(kind='scatter',x='index',y='obj_x',color='Red',label='G1')
在[2401]中:dff.plot(kind='scatter',x='index',y='obj_'y',color='Blue',label='G2',ax=ax)
Out[2401]:
您需要将两个数据帧合并为一个数据帧,然后可以使用已经找到的绘图提示。因此,您的问题实际上是关于连接/合并数据帧。关于SO-re:在中合并数据帧,已经有很多问题了pandas@PaulH谢谢我能够连接两个数据帧,并能够创建一个绘图。我的下一个挑战是缩小我的轴心。您能帮助我如何缩放y轴以显示1到1000之间的数据吗?关于设置matplotlib绘图的轴限制,可能有几十个问题。浏览其中一些,以及matplotlib+pandas文档,告诉我您发现了什么让人困惑。