[英]How to plot list if values with respect to its key of a dictionary in python
我有一本包含值列表的字典
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]]}
我需要 plot x 軸上的每個鍵和 y 軸上的值列表。
這是我的代碼:
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()
我收到以下錯誤。
---------------------------------------------------------------------------
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)
如果您的df_param
是形式的dict
:
{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.