python3:matplotlib用字典

时间:2017-02-26 08:20:53

标签: python python-3.x dictionary matplotlib plot

你好,有一本字典,m = {' A':[1.5,3.6,5.7,6,7,8],' B':[3.5,5,6 ,8,4,5],' C':[2.8,3.5,4.5,5.6,7.0,9.0]}。我想在一个图上用python matplotlib绘制三条线(如下图所示)。 x-aix是相同的:[1,2,3,4,5,6]。三个y值是关键(A,B,C)的值。 A,B,C是三行标签。如何绘制它。我已尝试过以下代码,但这是错误的,你能告诉我该怎么做。

sample for what I want

  for k, v in dict_s:
        plt(range(1, 4), v, '.-', label=k)

1 个答案:

答案 0 :(得分:5)

迭代字典只会生成密钥。

CumGPA: DSum("TermGPA","Transcript_Info","Sequence<=" &[Sequence])

如果您需要迭代(键,值)对,请使用dict.items()

>>> dict_s = {
...     'A': [1.5, 3.6, 5.7, 6, 7, 8],
...     'B': [3.5, 5, 6, 8, 4, 5],
...     'C': [2.8, 3.5, 4.5, 5.6, 7.0, 9.0]
... }
>>> for x in dict_s:
...     print(x)
...
A
C
B
>>> for x in dict_s.items():
...     print(x)
...
('A', [1.5, 3.6, 5.7, 6, 7, 8])
('C', [2.8, 3.5, 4.5, 5.6, 7.0, 9.0])
('B', [3.5, 5, 6, 8, 4, 5])

output figure