简体   繁体   English

如何 plot 列出 python 中字典键的值

[英]How to plot list if values with respect to its key of a dictionary in python

I have a dictionary with list of values我有一本包含值列表的字典

df_param = {};
for i in range(0,1000):
  df_param[i]=[[0]]
print(df_param)
df_param={0: [[0], [20], [20], [20], [5], [1], [5]], 1: [[0], [20], [20], [5], [1], [5]], 2: [[0], [20], [20], [5], [5]], 3: [[0], [20], [5], [5]], 4: [[0], [5], [5]], 5: [[0], [5]], 6: [[0]], 7: [[0]], 8: [[0], [20]], 9: [[0]], 10: [[0]]}

I need to plot each key in x-axis and the list of values in y-axis.我需要 plot x 轴上的每个键和 y 轴上的值列表。

在此处输入图像描述

this is my code:这是我的代码:

for i in range(0,1000):
  for j in range(0,10):
    curr = df1[j][i]['Classifier'][0]
    if(curr=='KNNClassifier'):
      df_param[i].append([df1[j][i]['Classifier'][1]['n_neighbors']])
print(df_param)
import matplotlib.pyplot as plt

import matplotlib.pyplot as plt
for key, values in df_param.items():
    plt.plot(key, values)
plt.show()

I get the below error.我收到以下错误。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-132-176b8a768873> in <module>
      1 import matplotlib.pyplot as plt
      2 for key, values in df_param.items():
----> 3     plt.plot(key, values)
      4 plt.show()

3 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    340 
    341         if x.shape[0] != y.shape[0]:
--> 342             raise ValueError(f"x and y must have same first dimension, but "
    343                              f"have shapes {x.shape} and {y.shape}")
    344         if x.ndim > 2 or y.ndim > 2:

ValueError: x and y must have same first dimension, but have shapes (1,) and (7, 1)

If your df_param is a dict of form:如果您的df_param是形式的dict

{x0: [[y0_a], [y0_b], ...], x1: [[y1_a], [y1_b], ...], ...} and you wish to make a scatter plot of all the (xk, yk_i) , then you can first make a proper xy array with two columns x and y : {x0: [[y0_a], [y0_b], ...], x1: [[y1_a], [y1_b], ...], ...}并且您希望对所有(xk, yk_i) ,然后您可以先创建一个包含两列xy的适当xy数组:

import numpy as np

xy = np.array([
    (x, y) for x, lst in df_param.items()
    for sublst in lst for y in sublst
])
plt.plot(*xy.T, 'o')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何将 map 值从列表传递到其相关字典键 - Python: How to map values from list to its related dictionary key 在字典中找到列表的键时,如何替换其值? 蟒蛇 - How do i replace values of a list when found in the dictionary with its key? python 如何更新将其值保存在列表中的字典键? - How to update a dictionary key that holds its values in a list? 如何根据列表中的值排序返回字典的键? - How to return a key of a dictionary based on sorting its values in a list? 带有键的字典中的总和-Python 2.7 - Sum Values in a Dictionary w/ respect to the Key - Python 2.7 Python,如何在每一行中打印字典键及其值? - Python, how to print dictionary key and its values in each line? 如何在匹配键时拆分python字典的值 - How to split a python dictionary for its values on matching a key 我如何 plot 一个 Python 字典键与其对应的字典值? - How do I plot a Python dictionary key against its corresponding dictionary value? 如何根据条件将第二个字典列表的键及其值添加到字典的第一个列表中 - How shall I add one of the key and its values of second list of dictionary to the first list of dictionary based on the condition 按字典对字典进行排序,然后在值相等的情况下对其键进行排序,然后输出列表 - Sorting a dictionary by its value, and then its key if values are equal then outputting a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM