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.
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)
CodePudding user response:
If your df_param
is a dict
of form:
{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
:
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')