![](/img/trans.png)
[英]Make a Scatter Plot in matplotlib with dates on x axis and values on y
[英]How to use matplotlib to plot 2 sets of (x,y) values on the same graph as a scatter plot (connected by lines)?
我有兩個 numpy arrays 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]]
我想 plot 這些 arrays 的 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 無論如何都是浮動 arrays 。
你在這里有兩個問題:
1:您使用的是plt.plot
而不是plt.scatter
的語法,因此第三個參數被解釋為標記大小的數字。 如果要連接點,則應使用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[:]
返回整個列表,因此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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.