Matplotlib绘图

2024-09-29 23:32:26 发布

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

我需要画两组100点的图。 第一组点沿着Y轴移动,下一组点离第一组点稍远一些。在

我的代码如下:

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

我的问题是两个列表,network和list,有不同的值,但是解释器将同一组点绘制两次图形。我需要两组不同的点,一组分别用于网络和列表。在

密码怎么了? 谢谢


Tags: numpytruedatareturnispltnetworkarray
1条回答
网友
1楼 · 发布于 2024-09-29 23:32:26

下面是一段代码,它将绘制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()

希望这有帮助

相关问题 更多 >

    热门问题