Mainland
Mainland

Reputation: 4564

Python plot a list of lists with different number of elements

I am giving an example of what I am trying to plot.

big_list = [[12,13,-12],[1,5,9,10,12],[-1,-2]]
plt.plot(big_list.index,big_list)
plt.show()

Output:

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

Upvotes: 2

Views: 1533

Answers (2)

Stef
Stef

Reputation: 30589

IIUC you want to plot all sub-lists on the same figure:

for list in big_list:
    plt.plot(list)

If not please comment. It's not clear to me what you want you your x axis.

Update based on comment:

In this case you can do:

from matplotlib.ticker import MaxNLocator
for i,list in enumerate(big_list):
    plt.scatter([i]*len(list), list)
plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))

enter image description here

Upvotes: 1

Mainland
Mainland

Reputation: 4564

After trial and error, I found the box plots is what I wanted. I have three sub-lists and box plot produces three indexes on x-axis and produces a box on y-axis in proportionate to the values in the sub-list.

My code:

plt.boxplot(big_list)
plt.show() 

Output:

enter image description here

Upvotes: 0

Related Questions