0

Can you take a look at my code and tell me the reason why I receive 'list index out of range' error?

fig, ax = plt.subplots(figsize=(10,5))
view_count = dodo_data['view_count']
like_count = dodo_data['like_count']
colors = ['r', 'k', 'b', 'g']

ax.legend()
ax.set_xlabel('View Count')
ax.set_ylabel('Like Count', rotation=0, labelpad=55)

for i in range(len(view_count)):
    plt.plot(view_count[i], like_count[i], 'o', color=colors[i])

My scatter plot is here

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
Reyhan
  • 105
  • 7
  • Which line produces the error? Can you copy and paste the stack trace here? – Cedric Nov 28 '19 at 06:38
  • 1
    Please share the values of view_count, like_count – Juhil Somaiya Nov 28 '19 at 06:39
  • @Cedced_Bro plt.plot(view_count[i], like_count[i], 'o', color=colors[i]) – Reyhan Nov 28 '19 at 06:41
  • can u share list counts of each one ? – Beyhan Gul Nov 28 '19 at 06:43
  • `for i in range(len(view_count))` - you assume that you have equal number of elements in view_count, like_count and colors. That seems not to be the case. – FObersteiner Nov 28 '19 at 06:44
  • @BeyhanGül I do not know the exact values, but given the scatterplot above I can say that: view_count = [180000, 290000, 310000, 380000] like_count = [4000, 15200, 14600, 16700] – Reyhan Nov 28 '19 at 06:53
  • Yes, @MrFuppes is right. You have more elements in view_count than in like_count. – Cool Breeze Nov 28 '19 at 06:53
  • @Rishabh How should I fix this? – Reyhan Nov 28 '19 at 06:54
  • @JuhilSomaiya I do not know the exact values, but given the scatterplot above I can say that: view_count = [180000, 290000, 310000, 380000] like_count = [4000, 15200, 14600, 16700] – Reyhan Nov 28 '19 at 06:54
  • @Rishabh you dont know that. maybe view_count and like_count have the same length but color doesnt – luigigi Nov 28 '19 at 06:56
  • Yes @luigigi , that can also be the case. Reyhan Please paste the traceback here, that would help debug this faster. https://stackoverflow.com/questions/3702675/how-to-print-the-full-traceback-without-halting-the-program – Cool Breeze Nov 28 '19 at 06:56
  • @luigigi View count and like count are 69-row-long. So their length is the same and much bigger than colors. – Reyhan Nov 28 '19 at 07:15
  • @Reyhan then you should know why get a "list is out of range" error. You can either use the same color for all points or you have to work on a better assignment of the colours – luigigi Nov 28 '19 at 07:23

2 Answers2

0

The problem is with the colors[i]. You have used i to iterate over the length of view_count column. Instead try using a separate iterator for indexing colors list which iterates over the len of colors list.

I've tried with a sample data , which I'm posting below. Hope it helps!!

from pandas import DataFrame

Data = {'view_count':  [1,2,3,4,5],
        'like_count': [6,7,8,9,10],
        }

dodo_data = DataFrame (Data, columns = ['view_count','like_count'])

print(dodo_data)

enter image description here

import matplotlib.pyplot as plt
import random

fig, ax = plt.subplots(figsize=(10,5))
view_count = dodo_data['view_count']
like_count = dodo_data['like_count']
colors = ['r', 'k', 'b', 'g']

ax.legend()
ax.set_xlabel('View Count')
ax.set_ylabel('Like Count', rotation=0, labelpad=55)

for i in range(len(view_count)):
    plt.plot(view_count[i], like_count[i], 'o', color=colors[random.sample(range(len(colors)),1)[0]])

enter image description here

Sathish
  • 16
  • 2
  • This works but I do not want to have a random sample. – Reyhan Nov 28 '19 at 07:13
  • Then make sure the lengths of colors list and view_count is same or alternatively, you can use different iterator for accessing colors list. – Sathish Nov 28 '19 at 08:53
0

You can do this:

for i in range(len(view_count)):
    plt.plot(view_count[i], like_count[i], 'o', color=colors[i%4])

You'll get output like this:

1 6 r
2 7 k
3 8 b
4 9 g
5 10 r
6 2 k
7 3 b
8 4 g
9 5 r
34 4 k
Cool Breeze
  • 738
  • 2
  • 10
  • 26