Setting Y Axis values on matplotlib chart, incrementing

Question:

Please may someone assist me on this. I am trying to add Y Axis values to my matplotlib chart so the chart looks like this Excel Chart:

enter image description here

However, on my matplotlib chart, the Y Axis values are from 0-6:

enter image description here

Is there some code I have left out here to add the values to the chart? Do I need to manually specify the values for them to be in increments of £10 million? I’m still very new to this sorry!

df_total_gross_sales.set_index('Date').plot.bar()

plt.title('Gross Sales: Discounted Vs Full Price', pad = 10, Fontsize =16)
plt.ylabel("Gross Sales (£)", labelpad = 10, fontsize=14)
plt.xlabel("Year-Month", labelpad = 20, fontsize=14)

plt.tight_layout()
plt.legend(facecolor='white', fontsize =13,  framealpha=1)
plt.show()

enter image description here

Asked By: Conor

||

Answers:

Just add

plt.gca().yaxis.set_major_formatter('₤{x:,}')

Somewhere before show.

A minimal reproducible example:

import numpy as np
import matplotlib.pyplot as plt

x=np.arange(30)
y=np.random.randint(5000000, 70000000, (30,))
plt.bar(x,y)
plt.gca().yaxis.set_major_formatter('₤{x:,}')
plt.show()

Gives
enter image description here

The same example, without that line, shows a y-axis with ticks labelled from 0 to 7, with, as in your example, in legend saying that those are ×10⁷ units.

Note that the ticks value in this format string is always named x (even tho it is y axis).

Answered By: chrslg
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.