0

我正在使用 for 循环在同一个 pd.plot.scatterplot 上分散多个数据帧,但每次循环返回时都会打印一个颜色条。如何在循环结束时只有一个颜色条?

这是我的代码

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

1 回答 1

2

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

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

在一个图中绘制了两个数据框的结果图像

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

于 2018-11-13T16:54:20.863 回答