python - 如何使用 matplotlib 在同一张图上绘制 2 组 (x,y) 值作为散点图(由线连接)?
问题描述
我有两个 numpy 数组 s1 和 s2,每个数组都包含一组 (x,y) 值。
例如
------s1-----
[[ 0.5 0. ]
[ 0.5 0.28284271]
[ 0.48 0.56568542]
[ 0.44 0.83721443]
[ 0.3808 1.08611602]
[ 0.304 1.30152903]
[ 0.211968 1.47349739]
[ 0.107776 1.5934046 ]
[-0.00489472 1.65437192]
[-0.12187648 1.65160304]
[-0.23866245 1.5826593 ]
[-0.35057336 1.44765143]
[-0.45293778 1.24933718]
[-0.54127926 0.99311688]
[-0.61150322 0.6869231 ]
[-0.66007602 0.34100464]
[-0.68418869 -0.03239075]
[-0.68189832 -0.41942632]
[-0.6522404 -0.80516626]
[-0.59530655 -1.17412915]
[-0.51228308 -1.51088539]]
-----s2----
[[-0.5 0. ]
[-0.5 0.28284271]
[-0.52 0.56568542]
[-0.56 0.83721443]
[-0.6192 1.08611602]
[-0.696 1.30152903]
[-0.788032 1.47349739]
[-0.892224 1.5934046 ]
[-1.00489472 1.65437192]
[-1.12187648 1.65160304]
[-1.23866245 1.5826593 ]
[-1.35057336 1.44765143]
[-1.45293778 1.24933718]
[-1.54127926 0.99311688]
[-1.61150322 0.6869231 ]
[-1.66007602 0.34100464]
[-1.68418869 -0.03239075]
[-1.68189832 -0.41942632]
[-1.6522404 -0.80516626]
[-1.59530655 -1.17412915]
[-1.51228308 -1.51088539]]
我想绘制这些数组的 x 和 y 值,以便得到如下结果:
我实现的代码片段看起来像这样
import matplotlib.pyplot as plt
.
.
.
.
plt.scatter(s1[:][0],s1[:][1],'-o',color='b')
plt.scatter(s2[:][0],s2[:][1],'-x',color='r')
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
我收到一条错误消息:
File "C:\Users\Acer\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2890, in scatter
__ret = gca().scatter(
File "C:\Users\Acer\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1438, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\Acer\anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py", line 411, in wrapper
return func(*inner_args, **inner_kwargs)
File "C:\Users\Acer\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 4488, in scatter
collection = mcoll.PathCollection(
File "C:\Users\Acer\anaconda3\lib\site-packages\matplotlib\collections.py", line 955, in __init__
self.set_sizes(sizes)
File "C:\Users\Acer\anaconda3\lib\site-packages\matplotlib\collections.py", line 922, in set_sizes
scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
我不明白出了什么问题,因为散点值中没有 sqrt 并且 s1 和 s2 无论如何都是浮点数组。
解决方案
你在这里有两个问题:
1:您使用的是 forplt.plot
而不是语法,plt.scatter
因此第三个参数被解释为标记 size的数字。如果你想连接你应该使用的点plt.plot
。
2a:假设s1
和s2
是numpy.ndarray
对象,您在方括号中包含所有索引并用逗号分隔它们。
#import numpy as np
#import matplotlib.pyplot as plt
s1 = np.array(s1)
s2 = np.array(s2)
plt.plot(s1[:,0],s1[:,1],'-o',color='b')
plt.plot(s2[:,0],s2[:,1],'-x',color='r')
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
2b:但是,如果它们是列表列表,则需要从每个成员列表的第一个/第二个元素构造列表。 s1[:]
with 返回整个列表,所以s1[:][0]
将返回整个列表的第一个元素,等于[0.5, 0]
.
使用列表推导可以做到这一点的一种方法是:
#import matplotlib.pyplot as plt
plt.plot(s1[:,0],s1[:,1],'-o',color='b')
plt.plot(s2[:,0],s2[:,1],'-x',color='r')
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
推荐阅读
- angularjs - jQuery Datatable ng-click 在表初始化时不起作用
- amazon-web-services - 如何确定哪个是正确的认知错误
- javascript - 将类添加到 innerHTML 属性
- django - Django Rest Framework:在子模型上添加超链接相关字段
- installation - ubuntu 20.04 上的 Emacs 27.1 找不到任何字体
- linux - 如何使用 socat 配置代理以侦听 MQTT 连接
- data-science - 有人可以帮我解决为什么我在网格搜索属性 cv_results_ 中获取 nan 值吗?
- intersystems-iris - 我是否需要为我的缓存实例启用 LDAP 身份验证,而不是在单个 CPS 应用程序中使用 LDAP 身份验证?
- reactjs - NextJS _app.js| 警告:无法对未安装的组件执行 React 状态更新
- javascript - 如何将动态日期时间选择器添加到我的应用程序?