2010-11-24 80 views
42

我想在同一散點圖繪製多個數據集:MatPlotLib:在相同的分散多個數據集劇情

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

上面只顯示最近scatter()

我也試過:

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

它在同一行套印。 – 2010-11-24 19:00:34

回答

63

您需要引用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

+4

`fig.add_subplot(111)`中的`111`是什麼意思? – Temak 2015-11-18 18:49:58

+0

這是圖中子圖的排列。第一個數字是多少排子圖;第二個數字是多少個子圖的列;第三個數字是你現在談論的子圖。在這種情況下,有一行和一列子圖(即一個子圖),軸正在談論第一個子圖。像fig.add_subplot(3,2,5)這樣的東西就是三行兩列網格中左下角的子圖。 – 2015-11-27 16:10:57

4

我不知道,這對我來說工作正常。確切的命令:

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

我碰到這個問題就來了,因爲我有確切同樣的問題。儘管接受的答案的作品不錯,但與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()