Bar chart starting out of axis in python

Viewed 42

I need to copy the bar chart in the image with python.

bar chart I have to copy

What I have been able to achieve is next image.

bar chart I have achieved

And the code I have used is:

import matplotlib.pyplot as plt

ausgaben = 130386
einnahmen = 147233
profit = einnahmen-ausgaben

titles = ["Ausgaben", "Profit", "Einnahmen"]
euros = [ausgaben, profit, einnahmen]

colors = ['#6F8CA7', '#F6BC06', '#59908F']
dummysum1 = []
dummysum2 = []

for i in range(len(euros)):
    dummysum1.append(euros[i]+4000)
    dummysum2.append(max(euros)+15000)
if euros[1] > 0:
    dummysum1[1] = euros[1]+4000
if euros[1] <= 0:
    dummysum1[1] = 4000

position1 = (euros[0]+euros[2])/2

percentile = (euros[2]-euros[0])/euros[0]*100

if percentile > 0:
    label0 = '+{:.1f}%'.format(percentile)
else:
    label0 = '{:.1f}%'.format(percentile)

fig, ax = plt.subplots(figsize=(7, 5))
fig.set_facecolor('#D0A210')
fig.patch.set_alpha(0.2)
ax.bar(titles[0], euros[0], alpha=0.6, color=colors[0])
ax.bar(titles[1], euros[1], alpha=0.6, color=colors[1])
ax.bar(titles[2], euros[2], alpha=0.6, color=colors[2])
plt.axhline(y=euros[0], color='#BCBCBC')
plt.axhline(y=euros[2], color='#BCBCBC')

ax.set_facecolor('#D0A210')
ax.patch.set_alpha(0.02)

ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

ax.spines.right.set_visible(False)
ax.spines.left.set_visible(False)
ax.spines.top.set_visible(False)
ax.spines.bottom.set_visible(False)

ax.text(titles[0], dummysum1[0], '{} €'.format(euros[0]), horizontalalignment='center')
ax.text(titles[1], dummysum1[1], '{} €'.format(euros[1]), horizontalalignment='center')
ax.text(titles[2], dummysum1[2], '{} €'.format(euros[2]), horizontalalignment='center')
ax.text(2.58, position1-1000, label0)

ax.text(titles[0], dummysum2[0], titles[0], horizontalalignment='center')
ax.text(titles[1], dummysum2[1], titles[1], horizontalalignment='center')
ax.text(titles[2], dummysum2[2], titles[2], horizontalalignment='center')

plt.show()

. How can I get the yellow bar chart starting at y=130386 instead of y=0 and the yellow arrow at the right hand side?

(The first question is the most important!)

Thank you all!

1 Answers

For the first question, just add a value for the bottom parameter. I have also added the arrow using annotate:

import matplotlib.pyplot as plt

ausgaben = 130386
einnahmen = 147233
profit = einnahmen-ausgaben

titles = ["Ausgaben", "Profit", "Einnahmen"]
euros = [ausgaben, profit, einnahmen]

colors = ['#6F8CA7', '#F6BC06', '#59908F']
dummysum1 = []
dummysum2 = []

for i in range(len(euros)):
    dummysum1.append(euros[i]+4000)
    dummysum2.append(max(euros)+15000)
if euros[1] > 0:
    dummysum1[1] = euros[1]+4000
if euros[1] <= 0:
    dummysum1[1] = 4000

position1 = (euros[0]+euros[2])/2

percentile = (euros[2]-euros[0])/euros[0]*100

if percentile > 0:
    label0 = '+{:.1f}%'.format(percentile)
else:
    label0 = '{:.1f}%'.format(percentile)

fig, ax = plt.subplots(figsize=(7, 5))
fig.set_facecolor('#D0A210')
fig.patch.set_alpha(0.2)
ax.bar(titles[0], euros[0], alpha=0.6, color=colors[0])
ax.bar(titles[1], euros[1], alpha=0.6, color=colors[1], bottom=ausgaben)
ax.bar(titles[2], euros[2], alpha=0.6, color=colors[2])
plt.axhline(y=euros[0], color='#BCBCBC')
plt.axhline(y=euros[2], color='#BCBCBC')

ax.set_facecolor('#D0A210')
ax.patch.set_alpha(0.02)

ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

ax.spines.right.set_visible(False)
ax.spines.left.set_visible(False)
ax.spines.top.set_visible(False)
ax.spines.bottom.set_visible(False)

ax.text(titles[0], dummysum1[0], '{} €'.format(euros[0]), horizontalalignment='center')
ax.text(titles[1], dummysum1[1]+ausgaben, '{} €'.format(euros[1]), horizontalalignment='center')
ax.text(titles[2], dummysum1[2], '{} €'.format(euros[2]), horizontalalignment='center')
ax.text(2.58, position1-1000, label0)

ax.text(titles[0], dummysum2[0], titles[0], horizontalalignment='center')
ax.text(titles[1], dummysum2[1], titles[1], horizontalalignment='center')
ax.text(titles[2], dummysum2[2], titles[2], horizontalalignment='center')

ax.annotate("", xy=(2.5, ausgaben+profit*1.05), xytext=(2.5, ausgaben), arrowprops=dict(arrowstyle="->", color="orange", lw=2.0))

plt.show()

enter image description here

Related