0

I have a dataframe as below (obtained after lot of preprocessing)

Please find code

{'token': {0: '180816_031', 1: '180816_031', 2: '180816_031', 3: '180816_031', 4: '180816_031', 5: '180816_031', 6: '180816_031', 7: '180816_031', 8: '180816_031', 9: '180816_031'}, 'variable': {0: 'Unnamed: 0', 1: 'adj_active_polymerase', 2: 'adj_functional_sequencing_pores', 3: 'adj_high_quality_reads', 4: 'adj_single_pores', 5: 'cell_mask_bilayers_sum', 6: 'num_align_high_quality_reads', 7: 'num_total_cells', 8: 'potential_pore', 9: 'short_pass'}, 'value': {0: 21.0, 1: 615850.51515151514, 2: 615850.51515151514, 3: 486008.39393939392, 4: 803784.06060606055, 5: 1665347.5757575757, 6: 468638.03030303027, 7: 2097152.0, 8: 1158527.0, 9: 2067189.2424242424}}

I am using below code to create my SNS bar plot but its throwing error, after graph I want to show standard deviation

df1 = df1.groupby(['token','variable']).agg({'value': 'mean'})
df1.reset_index(inplace=True)
g=sns.barplot(x='token',y='value',data=df1, color='variable')
plt.show()

The output that I want is as below

Dataframe

Dataframe Expected Output

However, I get an error Code on this

ValueError: Invalid RGBA argument: 'variable'
Vaibhav Singh
  • 1,159
  • 1
  • 10
  • 25

1 Answers1

1

First the mistake lies in the color parameter. It is hue parameter that do what you wanted. Check the code below:

#For sorting the values in descending order
df.sort_values('value',inplace=True,ascending=False)

fig,ax = plt.subplots()
fig.set_size_inches(16,8)

#to get different colors for each of the variable assign the variable to hue
g=sns.barplot(x='token',y='value',data=df, hue='variable',ax=ax)

#Code for to put legend outside the plot
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

# Adding respective values to the top of each bar
for p in ax.patches: 
    ax.annotate("%d" % p.get_height(), (p.get_x() + p.get_width() / 2, p.get_height()),
                ha='center', va='center', fontsize=11, color='black', xytext=(0, 10), 
                textcoords='offset points',fontweight='bold')

#To save the plot as 'SO.png'    
plt.savefig('SO.png',dpi=100,bbox_inches='tight')
plt.show()

Code for legend is from link.

The plot looks like:

Sample picture of the data provided as per OP's visualization

Space Impact
  • 13,085
  • 23
  • 48
  • Thanks @Sandeep, I have also added values on top of graph using below however how would I sort it from more to less as expected output ---------------------------------------- for p in ax.patches: ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', fontsize=11, color='gray', xytext=(0, 20), textcoords='offset points') – Vaibhav Singh Aug 24 '18 at 11:42
  • Thanks a lot Sandeep, Just one last thing which I am not able to figure out is that how to display the standard deviation whisker sort of displayed in expected output. Tried searching everywhere for same – Vaibhav Singh Aug 24 '18 at 12:01
  • @Artika If it helped you [please upvote and also accept the solution](https://stackoverflow.com/help/someone-answers). Glad to help. – Space Impact Aug 24 '18 at 12:04
  • Did that Sandeep, You were super helpful as a mentor to me. If I were to make a scatterplot with regression line as any idea where would I make change in the existing graph – Vaibhav Singh Aug 24 '18 at 12:10
  • @Artika For [scatterplot with regression line](https://seaborn.pydata.org/generated/seaborn.regplot.html#seaborn-regplot). But some of the above functionalities won't work. – Space Impact Aug 24 '18 at 12:25
  • Can you please help me to add error bars to this graph. Searched a lot but could not figure it out – Vaibhav Singh Aug 24 '18 at 17:04