Issue
My input is a dataframe df
(you can find a snippet in the end of my question) and I try to create a plot like the second 'Panel vendor bar chart' here : https://peltiertech.com/stacked-bar-chart-alternatives
The code I made is this (by the way I'm open to any suggestations to improve it) :
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('my_folder/list_of_softwares.xlsx')
sums = df.groupby('Software').sum().T
fig, axes = plt.subplots(1, len(sums.index), sharey=True, figsize=(12, 3))
plt.subplots_adjust(wspace=0)
colors = ['#6caddf', '#f27077', '#9dd374', '#fab26a']
for ax, software, parameter, color in zip(axes, sums.columns, sums.index, colors):
ax.barh(sums.loc[parameter].index, sums.loc[parameter].values, color=color)
ax.set(xticks=range(0, df.drop('Software', axis=1).max().max()+1, 2000), title=parameter)
fig.suptitle('Summary of Softwares', y=1.05, fontweight='bold')
plt.show()
It works except 4 small issues :
- The
y
ticks in all axes should be removed - The subtitles of the axes should be inside the figure and not outside it
- Some
x
ticks labels are overlapping - The vertical line that separate each ax should be removed
I feel like this can be done easily but I don't know how.
Any ideas, guys ?
df
look like this :
Software Parameters Reports Dashbords Scorecards
0 Tableau 7935 68 474 712
1 Tableau 69 518 695 122
2 Oracle 651 540 842 764
3 Oracle 700 52 776 948
4 Oracle 758 862 182 757
5 IBM 999 271 847 338
6 IBM 316 128 395 441
7 IBM 915 199 1000 747
8 IBM 44 685 818 427
9 Tibco 500 575 876 450
10 Board 748 936 367 771
11 Board 304 700 856 77
12 Board 623 974 120 802
13 Board 131 151 395 410
14 Board 217 645 996 537
15 LogiXML 630 779 752 433
16 LogiXML 947 391 280 109
Solution
You only have to adjust some simple individual settings of the ax
object, which is each individual plot:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_clipboard() # copy data in the SO question
sums = df.groupby('Software').sum().T
fig, axes = plt.subplots(1, len(sums.index), sharey=True, figsize=(12, 3))
plt.subplots_adjust(wspace=0.2)
colors = ['#6caddf', '#f27077', '#9dd374', '#fab26a']
for ax, software, parameter, color in zip(axes, sums.columns, sums.index, colors):
ax.barh(sums.loc[parameter].index, sums.loc[parameter].values, color=color)
ax.set(xticks=range(0, df.drop('Software', axis=1).max().max()+1, 2000), )
ax.set_title(parameter, y=0.9)
ax.yaxis.set_tick_params(length=0)
for d in ["top", "bottom", "right", "left"]:
ax.spines[d].set_visible(False)
fig.suptitle('Summary of Softwares', y=1.05, fontweight='bold')
plt.show()
Answered By - jjsantoso
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.