【问题标题】:Multi line graph with matplotlib使用 matplotlib 的多线图
【发布时间】:2019-12-11 21:11:09
【问题描述】:

假设:

A=[[1,4,2], [2,3,5],[2,4,5]]...(此列表中有φ列表)

B=[date1, date2, date3,...](φ 日期)

基本上A[I] 对应B[I] 中的日期,A 内部有一个列表,其中包含product1, product2, product3 在该日期B[I] 的价格。

我想制作一个以日期为 X 轴、价格为 Y 轴的多线图。所以每个日期会有 3 个点(产品 1 的价格,产品 2 的价格,产品 3 的价格)。

还有一个问题,在A的一些列表中,'inside'列表中没有3个产品,只有2个。因为另一个没有注册

例如:A= [[1,4,2],[2,7], [2,4,5]]

说的多线图怎么办?

【问题讨论】:

  • 当只有 2 个价格时,是否总是缺少相同的产品?
  • 哪个价格没有注册?是产品 1、产品 2 还是产品 3?你怎么知道?
  • 不一定,每个内部列表都可能缺少产品。但它往往是完整的

标签: python matplotlib graph


【解决方案1】:

让我开始指出,这不是存储数据的好方法。 如果其中一个价格未注册,则无法分辨是哪一个,这会导致整个条目无法使用。最好使用None 之类的占位符来记住未测量的值。例如:

[5, 6, None] --> The entry for the third product is missing
[5, None, 5] --> The entry for the second product is missing
[None, 3, 5] --> The entry for the first product is missing

也就是说,我将分享一种方法,假设它始终是最后一个丢失的产品。通过使用占位符重新记录您的数据,它应该会给您正确的结果。

import matplotlib.pyplot as plt
import numpy as np

total_dates = 10

# create dummy list
A = []
for a in range(total_dates):
    A.append([np.random.randint(10) for a in range(np.random.randint(2,4))])

# create dummy dates with same length
B = range(total_dates)

# make all inside lists the same length
for sublist in A:
    while len(sublist) < 3:
        sublist += [None]

# plot as lineplot
for prices in list(zip(*A)):
    plt.plot(B, A)

plt.show()

【讨论】:

    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2018-04-02
    • 2014-07-27
    • 2018-08-13
    相关资源
    最近更新 更多