0

I'm trying to construct a simple bar chart using matplotlib. My code is :

labels = ['A','B','C']
values = [70000,80000,90000]
c = ['#FFC000','#5B9BD5','#404040','#5B9BD5']

barplot = plt.bar(labels,values,width=0.3,color = c)
 
# setting value in middle    
for i in range(len(values)):
    plt.annotate(str(values[i]), xy=(labels[i],values[i]), ha='center', va='bottom')

plt.ylabel('Savings') 
plt.ylim(50000,100000,5000) # yticks for bar chart
plt.grid(which='major', axis='y', zorder=-1.0) # greenline but hidden behind the bar

plt.show()

Now when I run it on Google Colab, the axis label lines are overlaping with my bar chart; Although Jupyter notebooks do not have this problem.

You can see the problem more clearly with this Colab Link.

Please suggest any solution to fix this out.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Kanchon Gharami
  • 777
  • 1
  • 11
  • 30
  • I ran your code in my jupyterLab environment, and it is identical to the diagram output by Colab. jupyterLab is 3.0.16. What does your hand output look like? – r-beginners Sep 12 '21 at 07:41
  • I got the desired result from the Jupiter notebook, the level of the bar's layer is above the horizontal axis line. But in Collab the horizontal axis line comes above the bar which looks very unprofessional. – Kanchon Gharami Sep 12 '21 at 07:49
  • This can be achieved by setting the drawing order to 2 for bars and 1 for grid. What version of maplotlib do you have? – r-beginners Sep 12 '21 at 07:59
  • 1
    See this [page](https://matplotlib.org/stable/gallery/misc/zorder_demo.html). – r-beginners Sep 12 '21 at 08:03
  • thanks, maybe the `zorder` can solve my problem. – Kanchon Gharami Sep 12 '21 at 08:13
  • Does this answer your question? [How to draw grid lines behind matplotlib bar graph](https://stackoverflow.com/questions/23357798/how-to-draw-grid-lines-behind-matplotlib-bar-graph) – Trenton McKinney Sep 12 '21 at 14:24

0 Answers0