0

Python 3.7 environent

I want to create a stacked bar plot with some labels on top of each subcategory displyed as the bar. The data comes from a CSV file, and some of the labels are rather long, so they are larger than the bar width. The problem could be easily solved by scaling the whole graphic such that the bars become large enough for the labels, but I fail to re-size the plot as a whole. here the code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()

dataset = 'Number'

dataFrame: pd.DataFrame = pd.read_csv('my_csv_file_with_data.csv', sep=',', header=2)
dataFrame['FaultDuration [h]'] = dataFrame['DurationH']

# ***********************************************************
# Data gymnastics to transform data in desired format
# determine the main categories
mainCategories: pd.Series = dataFrame['MainCategory']
mainCategories = mainCategories.drop_duplicates()
mainCategories = mainCategories.sort_values()
print('Main Categories: '+ mainCategories)

# subcategories
subCategories: pd.Series = pd.Series(data=dataFrame['SubCategorie'].drop_duplicates().sort_values().values)
subCategories = subCategories.sort_values()
print('Sub Categories: '+ subCategories)

# Build new frame with subcategories as headers
columnNames = pd.Series(data=['SubCategory2'])
columnNames = columnNames.append(mainCategories)
rearrangedData: pd.DataFrame = pd.DataFrame(columns=columnNames.values)

for subCategory in subCategories:
    subset: pd.DataFrame = dataFrame.loc[dataFrame['SubCategorie'] == subCategory]
    rearrangedRow = pd.DataFrame(columns=mainCategories.values)
    rearrangedRow = rearrangedRow.append(pd.Series(), ignore_index=True)
    rearrangedRow['SubCategory2'] = subCategory
    for mainCategory in mainCategories:
        rowData: pd.DataFrame = subset.loc[subset['MainCategorie'] == mainCategory]
        if (rowData is not None and rowData.size > 0):
            rearrangedRow[mainCategory] = float(rowData[dataset].values)
        else:
            rearrangedRow[mainCategory] = 0.0

    rearrangedData = rearrangedData.append(rearrangedRow, ignore_index=True)

# *********************************************************************
# here the plot is created:
thePlot = rearrangedData.set_index('SubCategory2').T.plot.bar(stacked=True, width=1, cmap='rainbow')
thePlot.get_legend().remove()

labels = []

# *************************************************************
# creation of bar patches and labels in bar chart
rowIndex = 0
for item in rearrangedData['SubCategory2']:
        colIndex = 0
        for colHead in rearrangedData.columns:
            if colHead != 'SubCategory2':
                if rearrangedData.iloc[rowIndex, colIndex] > 0.0:
                    label = item + '\n' + str(rearrangedData.iloc[rowIndex, colIndex])
                    labels.append(item)
                else:
                    labels.append('')

            colIndex = colIndex + 1
        rowIndex = rowIndex + 1

patches = thePlot.patches

for label, rect in zip(labels, patches):
    width = rect.get_width()
    if width > 0:
        x = rect.get_x()
        y = rect.get_y()
        height = rect.get_height()
        thePlot.text(x + width/2., y + height/2., label, ha='center', va='center', size = 7 )

# Up to here things work like expected...
# *******************************************************
# now I want to produce output in the desired format/size

# things I tried:
1)    thePlot.figure(figsize=(40,10)) <---- Fails with error 'Figure' object is not callable
2)    plt.figure(figsize=(40,10)) <---- Creates a second, empty plot of the right size, but bar chart remains unchanged
3)    plt.figure(num=1, figsize=(40,10)) <---- leaves chart plot unchanged

plt.tight_layout()
plt.show()

The object "thePlot" is an AxesSubplot. How do I get to a properly scaled chart?

WolfiG
  • 1,059
  • 14
  • 31

1 Answers1

1

You can use the set sizes in inches:

theplot.set_size_inches(18.5, 10.5, forward=True)

For example see: How do you change the size of figures drawn with matplotlib?

RVA92
  • 666
  • 4
  • 17
  • Almost, the link did the trick. I had to do ```fig = plt.gcf() fig.set_size_inches(10, 6)``` – WolfiG May 08 '20 at 14:47