簡體   English   中英

Plot 散點圖上只有“非無”值

[英]Plot only "not None" values at a scatter graph

我有 2 個列表:

list1 = [-1, -6, None, -8, None, None, -2] #axis y
list2 = [1,2,3,4,5,6,7] #axis x

我需要 plot 在散點圖上只有非無值(-1,-6 ...)但同時在 X 軸上顯示 list2 上的所有值。

我還需要保持 Y 軸的順序與所示順序相同(-1、-6...)。 有誰可以幫助我嗎?

要過濾掉list1中的所有None值,您可以使用這樣的代碼片段

does_not_contain_none = list(filter(lambda x: x is not None, list1))

注意:這將保留元素的原始順序

一步步

zipped = zip(list1, list2)  # make pairs: (-1, 1), (-6, 2), (None, 3), ...
filtered = filter(lambda x: x[0] is not None, zipped)  # filter all pairs where first element is None
ys, xs = zip(*filtered)  # reverse zip operation but on the filtered data

更緊湊

filtered = (pair for pair in zip(list1, list2) if pair[0] is not None)
ys, xs = zip(*filtered)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM