在同一个p中分散多个数据帧

2024-04-25 23:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用for循环将多个数据帧分散在同一个数据帧上pd.plot.散点图,但每次循环返回时,它都会打印一个色条。 如何在循环结束时只有一个色条?你知道吗

这是我的密码

if colormap is None: colormap='jet'
f,ax = plt.subplots()
for i, data in enumerate(wells):
    data.plot.scatter(x,y, c=z, colormap=colormap, ax=ax)
ax.set_xlabel(x); ax.set_xlim(xlim)
ax.set_ylabel(y); ax.set_ylim(ylim)
ax.legend()
ax.grid()
ax.set_title(title)

Tags: 数据密码fordataifplottitleis
1条回答
网友
1楼 · 发布于 2024-04-25 23:27:41

这可以通过使用图并将轴添加到同一子图中来实现:

import pandas as pd
import numpy as np

# created two dataframes with random values
df1 = pd.DataFrame(np.random.rand(25, 2), columns=['a', 'b'])
df2 = pd.DataFrame(np.random.rand(25, 2), columns=['a', 'b'])

然后:

fig = plt.figure()
for i, data in enumerate([df1, df2]):
    ax = fig.add_subplot(111)
    ax = data.plot.scatter(x='a', y='b', ax=ax,
                           c='#00FF00' if i == 0 else '#FF0000')

plt.show()

Resulting image with two dataframes plotted in one figure

您可以根据需要添加标签和其他元素。你知道吗

相关问题 更多 >