I have a table like below, which is stored in pandas dataframe called 'data'.
Column1 | Device1 | event_rate % | % event dist | % non-event dist | % total dist |
---|---|---|---|---|---|
0 | Android | 3.08 | 27.3 | 32.96 | 32.75 |
1 | Chrome OS | 4.05 | 0.47 | 0.42 | 0.43 |
2 | Chromium OS | 9.95 | 0.23 | 0.08 | 0.09 |
3 | Linux | 2.27 | 0.05 | 0.09 | 0.09 |
4 | Mac OS | 6.43 | 4.39 | 2.45 | 2.52 |
5 | Others | 2.64 | 7.41 | 10.48 | 10.36 |
6 | Windows | 5.7 | 15.89 | 10.08 | 10.3 |
7 | iOS | 3.76 | 44.26 | 43.44 | 43.47 |
I am trying to create a desired seaborn/matplot chart like shown below which was created in excel.
Here is my python code:
feature = 'Device1'
fig, ax1 = plt.subplots(figsize=(10,6))
color = 'tab:blue'
title = 'Event rate by ' + feature
ax1.set_title(title, fontsize=14)
ax1.set_xlabel(feature, fontsize=14)
ax2 = sns.barplot(x=feature, y='% non-event dist', data = data, color=color)
ax2 = sns.barplot(x=feature, y='% event dist', data = data, color='orange')
plt.xticks(rotation=45)
ax1.set_ylabel('% Dist', fontsize=14, color=color)
ax1.tick_params(axis='y')
ax2 = ax1.twinx()
color = 'tab:red'
ax2.set_ylabel('Event Rate %', fontsize=14, color=color)
ax2 = sns.lineplot(x=feature, y='event_rate %', data = data, sort=False, color=color)
ax2.tick_params(axis='y', color=color)
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
handles = handles1 + handles2
labels = labels1 + labels2
plt.legend(handles,labels)
plt.show()
Here is what I get
Issues:
- Legend is not showing.
- The barplots are overlapping each other.
- Is there a way to show data labels?
How can I make my seaborn plot look similar to my excel plot? Thanks.