Given a series of positive and negative values representing financial adjustments to a base value (e.g., starting profits, expenses, gains, losses, ending profits), create a waterfall chart showcasing how each adjustment contributes to the final value.
Example Output:
For the waterfall chart, break down your data into starting value, adjustments, and ending value. You can use the bar
function of Matplotlib to create bars, and add lines between them to showcase flow. Remember to adjust the bar colors based on positive or negative adjustments.
import matplotlib.pyplot as plt def waterfall_chart(adjustments, labels): final_values = [adjustments[0]] for value in adjustments[1:]: final_values.append(final_values[-1] + value) fig, ax = plt.subplots() bars = ax.bar(range(len(adjustments)), final_values, color='blue') ax.plot(range(len(adjustments)), final_values, color='red') for bar, value in zip(bars, adjustments): height = bar.get_height() ax.text(bar.get_x() + bar.get_width() / 2, height / 2, f'{value}', ha='center', color='white') plt.xticks(range(len(adjustments)), labels, rotation=45) plt.title("Waterfall Chart of Financial Adjustments") plt.show() # Example usage adjustments = [1000, -300, 200, -150, 400, -250, 1500] labels = ['Start', 'Expense 1', 'Gain 1', 'Expense 2', 'Gain 2', 'Expense 3', 'End'] waterfall_chart(adjustments, labels)
Unlock AI & Data Science treasures. Log in!