Skip to content

Commit

Permalink
spotify#80 resolution
Browse files Browse the repository at this point in the history
I fixed the issue users were having of not being able to display column totals onto bar stacked graphs I did this in a very non-intrusive way by using different charting functions and various panda library calls to slice the data frame add each individual fruit together then add it back into the datagram and set the totals into a subtitle
  • Loading branch information
a18sullivan authored May 5, 2022
1 parent 7e55cc4 commit 75ea87a
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions examples/barStackedTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import numpy as np
import pandas as pd
import chartify

data = chartify.examples.example_data()

quantity_by_fruit_and_country = (data.groupby(
['fruit', 'country'])['quantity'].sum().reset_index())

country_order = (
quantity_by_fruit_and_country.groupby('country')['quantity'].sum()
.sort_values(ascending=False).index)

quantity_by_fruit_and_country['label'] = np.where(
quantity_by_fruit_and_country['fruit'].isin(['Banana', 'Orange', 'Grape', 'Apple']),
quantity_by_fruit_and_country['quantity'],
None)

# apple total=608
apples = quantity_by_fruit_and_country[quantity_by_fruit_and_country['fruit'] == "Apple"]
appleQuantity = apples['quantity']
appleTotal = appleQuantity.sum()

banana = quantity_by_fruit_and_country[quantity_by_fruit_and_country['fruit'] == "Banana"]
bananaQuantity = banana['quantity']
bananaTotal = bananaQuantity.sum()

grape = quantity_by_fruit_and_country[quantity_by_fruit_and_country['fruit'] == "Grape"]
grapeQuantity = grape['quantity']
grapeTotal = grapeQuantity.sum()

orange = quantity_by_fruit_and_country[quantity_by_fruit_and_country['fruit'] == "Orange"]
orangeQuantity = orange['quantity']
orangeTotal = orangeQuantity.sum()

# print(quantity_by_fruit_and_country)
quantity_by_fruit_and_country.insert(4, "fruit total", [appleTotal, None, None, None, None,
bananaTotal, None, None, None, None,
grapeTotal, None, None, None, None,
orangeTotal, None, None, None, None], False)

ch = chartify.Chart(blank_labels=True, x_axis_type='categorical')
ch.set_title("Grouped bar chart - Ordered stack")
ch.set_subtitle("Banana Total: " + str(bananaTotal) +
" Orange Total: " + str(orangeTotal) +
" Apple Total: " + str(appleTotal) +
" Grape Total: " + str(grapeTotal))
ch.plot.bar_stacked(
data_frame=quantity_by_fruit_and_country,
categorical_columns=['fruit'],
numeric_column='quantity',
stack_column='country',
normalize=False,
stack_order=country_order)

ch.plot.text_stacked(
data_frame=quantity_by_fruit_and_country,
categorical_columns=['fruit'],
numeric_column='quantity',
stack_column='country',
text_column='label',
normalize=False,
stack_order=country_order,
# Set the text color otherwise it will take
# The next color in the color palette.
text_color='white'
)

ch.show('html')

0 comments on commit 75ea87a

Please sign in to comment.