【问题标题】:Plot list of list with different size绘制不同大小的列表列表
【发布时间】:2020-02-04 03:38:57
【问题描述】:

给定一个列表,我尝试用不同的颜色绘制每一行,但出现错误:

ValueError: x 和 y 的大小必须相同

lists = 

    0     [[44.9597, 7.7825], [44.9646, 7.7816], [45.1206, 7.7115]]
    1     [[45.0021, 7.6733], [45.003, 7.6678]]
    2     [[45.0746, 7.5985], [45.0735, 7.5956],[44.9706, 7.704],[45.0811, 7.5511]
    3     [[45.1205, 7.7116]] 

因此,在每一行中,[] 中的第一个数字是 y,第二个数字是 x

我试过matplotlibseaborn

【问题讨论】:

  • 您能提供更多信息吗? x 值是索引,y 值是列表吗?还是每个列表列表中的 x 和 y 值?如果您可以为其中一行提供一个图,也可能会有所帮助,以便我们知道您要做什么
  • @IanThompson 好的。因此,在每一行中,[ ] 中的第一个数字是 y,第二个数字是 x

标签: python pandas list matplotlib seaborn


【解决方案1】:

您可以将matplotlibs scatter(x,y) 函数与numpy 一起使用:

import numpy as np
import matplotlib.pyplot as plt

lists = np.array([[[44.9597, 7.7825], [44.9646, 7.7816], [45.1206, 7.7115]], \
                  [[45.0021, 7.6733], [45.003, 7.6678]], \
                  [[45.0746, 7.5985], [45.0735, 7.5956],[44.9706, 7.704],[45.0811, 7.5511]], \
                  [[45.1205, 7.7116]]])

plt.figure()
for l in lists:
    ar = np.array(l)  # convert to numpy array for use of indices in next line
    plt.scatter(ar[:,1], ar[:,0]) 
plt.show()

【讨论】:

  • 问题说“第一个数字是 y,第二个数字是 x”。翻转散点坐标。
  • @IanThompson 我发布答案时没有,但是:现在快乐吗? ;)
猜你喜欢
  • 2021-12-11
  • 2012-06-01
  • 2021-10-13
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
相关资源
最近更新 更多