Python MatPlotLib:同一散点图上的多个数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4270301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:00:11  来源:igfitidea点击:

MatPlotLib: Multiple datasets on the same scatter plot

pythonscipymatplotlib

提问by Austin Richardson

I want to plot multiple data sets on the same scatter plot:

我想在同一个散点图上绘制多个数据集:

cases = scatter(x[:4], y[:4], s=10, c='b', marker="s")
controls = scatter(x[4:], y[4:], s=10, c='r', marker="o")

show()

The above only shows the most recent scatter()

以上只显示最近的 scatter()

I've also tried:

我也试过:

plt = subplot(111)
plt.scatter(x[:4], y[:4], s=10, c='b', marker="s")
plt.scatter(x[4:], y[4:], s=10, c='r', marker="o")
show()

采纳答案by nate c

You need a reference to an Axesobject to keep drawing on the same subplot.

您需要对Axes对象的引用才能继续在同一个子图上绘图。

import matplotlib.pyplot as plt

x = range(100)
y = range(100,200)
fig = plt.figure()
ax1 = fig.add_subplot(111)

ax1.scatter(x[:4], y[:4], s=10, c='b', marker="s", label='first')
ax1.scatter(x[40:],y[40:], s=10, c='r', marker="o", label='second')
plt.legend(loc='upper left');
plt.show()

enter image description here

在此处输入图片说明

回答by Steve Tjoa

I don't know, it works fine for me. Exact commands:

我不知道,它对我来说很好用。确切的命令:

import scipy, pylab
ax = pylab.subplot(111)
ax.scatter(scipy.randn(100), scipy.randn(100), c='b')
ax.scatter(scipy.randn(100), scipy.randn(100), c='r')
ax.figure.show()

回答by MaVe

You can also do this easily in Pandas, if your data is represented in a Dataframe, as described here:

如果您的数据在 Dataframe 中表示,您也可以在 Pandas 中轻松完成此操作,如下所述:

http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html#scatter-plot

http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html#scatter-plot

回答by Sohaib Farooqi

I came across this question as I had exact same problem. Although accepted answer works good but with matplotlib version 2.1.0, it is pretty straight forward to have two scatter plots in one plot without using a reference to Axes

我遇到了这个问题,因为我遇到了完全相同的问题。虽然接受的答案效果很好,但使用 matplotlib 版本2.1.0,在一个图中有两个散点图而不使用对Axes

import matplotlib.pyplot as plt

plt.scatter(x,y, c='b', marker='x', label='1')
plt.scatter(x, y, c='r', marker='s', label='-1')
plt.legend(loc='upper left')
plt.show()