[英]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)
,然后您可以先创建一个包含两列x
和y
的适当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.