I created the following dictionary below (mean_task_dict
). This dictionary includes three keys associated with three lists. Each lists includes 48 numeric values.
mean_task_dict = {
"Interoception": task_mean_intero,
"Exteroception": task_mean_extero,
"Cognitive": task_mean_cognit,
}
I would like to plot the values contained in each list inside a scatterplot where the x-axis comprises three categories (ROI_positions = np.array([1, 2, 3])
).
Each of the respective lists in the dictionary has to be linked to one of the categories from ROI_positions
above.
Here is my current attempt or code for this task:
import numpy as np
import matplotlib.pyplot as plt
task_mean_intero = [-0.28282956438352846, -0.33826908282117457, -0.23669673649758388]
task_mean_extero = [-0.3306686353702893, -0.4675910056474869, -0.2708033871055369]
task_mean_cognit = [-0.3053766849270014, -0.41698707094527254, -0.35655464189810543]
mean_task_dict = {
"Interoception": task_mean_intero,
"Exteroception": task_mean_extero,
"Cognitive": task_mean_cognit,
}
for value in mean_task_dict.values():
ROI_positions = np.array([1, 2, 3])
data_ROIs = np.array([
mean_task_dict["Interoception"][1],
mean_task_dict["Exteroception"][1],
mean_task_dict["Cognitive"][1]
])
plt.scatter(ROI_positions, data_ROIs)
My problem is that I am only able to compute and plot the data for one value by paradigmatically selecting the second index value of each list [1]
.
How can I loop through all values inside the three lists nested in the dictionary, so that I can plot them all together in one plot?
CodePudding user response:
Do you want something like this?
ROI_positions = np.array([1, 2, 3])
for i in range(len(mean_task_dict)):
data_ROIs = np.array([
mean_task_dict["Interoception"][i],
mean_task_dict["Exteroception"][i],
mean_task_dict["Cognitive"][i]
])
plt.scatter(ROI_positions, data_ROIs)
plt.show()
To be independent of dict size, you could do this,
ROI_positions = np.arange(len(mean_task_dict))
data_ROIs = np.array(list(zip(*mean_task_dict.values())))
for i in range(len(mean_task_dict)):
plt.scatter(ROI_positions, data_ROIs[i])
plt.show()
CodePudding user response:
Use a nested loop that iterates over each element of the dictionary and then iterates over each list item of that dictionary element.
for value in mean_task_dict.values():
for item in value:
#do stuff here