2013-06-23 61 views
0

我需要绘制两组每个100个点。 第一组点沿Y轴排列,下一组点距离第一组点较远。Matplotlib绘图

我的代码如下:

import matplotlib.pyplot as plt 
data= numpy.array(network)  #network is a list of values 
datatwo= numpy.array(list)  #list is another list 
cmap= numpy.array([(1,0,0),(0,1,0)]) 
uniqdata, idx=numpy.unique(data, return_inverse=True) 
uniqdata, idx=numpy.unique(datatwo, return_inverse=True) 

N=len(data) 
M=len(datatwo) 
fig, ax=plt.subplots() 
plt.scatter(numpy.zeros(N), numpy.arange(1,N+1), s=50, c=cmap[idx]) 
plt.scatter(numpy.ones(M), numpy.arange(1,M+1), s=50, c=cmap[idx]) 
plt.grid() 
plt.show() 

我的问题是,这两个名单,网络和列表,有不同的价值观,但解释的图形相同的一组点两倍。我需要有两组不同的点,分别用于网络和列表。

代码有什么问题? 谢谢

+0

小评论'matplotlib.pyplot在plt'应该是'as'不'at' – Greg

+5

嗯,首先,你是不是使用'data'或'datatwo'你的情节呼吁。你只是绘制它们的长度(即,如果'数据'的长度为3,则将[1,2,3]绘制为y值,而不管数据的实际值是多少)。另外,您在创建后立即覆盖'idx'。 – BrenBarn

+0

你不应该使用'list'命名一个变量,但可能无法解决你的问题。 –

回答

0

这是一段代码,它将绘制包含在2个列表中的唯一值,第一组位于Y轴,第二组位于Y = 1,每个列表使用不同的颜色。我在猜测,因为您使用的是np.unique,这两个列表包含您不想多次绘制的重复值。

import numpy as np 
import matplotlib.pyplot as plt 
#################################################################################### 

network = [1,2,3,4,5,6,7,8,8,8,9,10] # Some lists of values 
ilist = [1,2,3,4,5,6,7,8,9,9,9,10] # Some other list of values 


data= np.array(network)  #network is a list of values 
datatwo= np.array(ilist)  #list is another list 

# Some list of color maps to color each list with 
cmap= np.array([(1,0,0),(0,1,0)]) 

# Get the unique values from each array 
uniqdata1, idx1=np.unique(data, return_inverse=True) 
uniqdata2, idx2=np.unique(datatwo, return_inverse=True) 

# Find the length of each unique value array 
N=len(uniqdata1) 
M=len(uniqdata2) 

# Plot the data 
fig, ax=plt.subplots() 
plt.scatter(np.zeros(N), uniqdata1, s=50, c=cmap[0]) 
plt.scatter(np.ones(M), uniqdata2, s=50, c=cmap[1]) 
plt.grid() 
plt.show() 

希望这有助于