0

I have created a python function to draw a violin plot using plotly. The code for that is:

    def print_violin_plot(data1,data2,type1,type2,fig_title):
        data = pd.DataFrame(columns=('Group', 'Number'))
        for i in data1:
            x = pd.DataFrame({'Group' : [type1],
                      'Number' : [i],})
        data = data.append(x, ignore_index=True) 
        for i in data2:
            x = pd.DataFrame({'Group' : [type2],
                      'Number' : [i],})
        data = data.append(x, ignore_index=True)  

       fig = FF.create_violin(data, data_header='Number',     
       group_header='Group', height=500, width=800, title= fig_title)
       py.iplot(fig, filename="fig_title")

I then call the function

    print_violin_plot(eng_t['grade'],noneng_t['grade'],'English 
    Speaking','Non-English Speaking', 'Grades')

The function runs fine, without any errors, but I dont see any output. Why?

SZA
  • 71
  • 2
  • 2
  • 3

1 Answers1

0

When you loop over data1 and data2 you assign new instances of pd.DataFrame to the variable x and with each iteration you overwrite the x. If you want to append the instances of pd.DataFrame you should move the data.append(x) to the for-loop. Also, data.append() returns None, it's a method working on the data object, so data = data.append(..) results in data == None. Your code should look like this

data = pd.DataFrame(columns=('Group', 'Number'))
for i in data1:
        x = pd.DataFrame({'Group' : [type1],
                  'Number' : [i],})
for i in data2:
        x = pd.DataFrame({'Group' : [type2],
                  'Number' : [i],})
        data.append(x, ignore_index=True) 

To be clear, I must say that I have no idea what pg.DataFrame means or does, but what I wrote above is for sure a flaw in your code. I assumed that pg.DataFrame instances support append method and work just like lists.

marcinowski
  • 349
  • 2
  • 4