Hello I want to change the highlighted values at y axis from 0 to 10.000.000 with an incrementation of 1.000.000, first here's the data
I don't know how to change the y ticklabel (because there are 3 graphs within the plot) the and most of the examples on the internet use variable and not dataframe, here's the code, should I use numpy and input the value to a variable?
plt.figure(figsize=(15,4))
#ax.ticklabel_format(style='plain')
#plt.ticklabel_format(useOffset=None)
plt.plot(df['MONTH'], df['MONTHLY INCOME'])
plt.plot(df['MONTH'], df['MONTHLY EXPENSES'])
plt.plot(df['MONTH'], df['MONTHLY SAVINGS'])
plt.ylabel("Dollar")
plt.xlabel("Month")
plt.legend(legend_labels)
CodePudding user response:
you can do this:
plt.yticks(np.arange(0,11e6,1e6),np.arange(0,11e6,1e6))
The first arguments sets the position of the tick, the second one is cast into string, so it is the label. You could also do something like:
plt.yticks(np.arange(0,11e6,1e6),[str(x) 'M' for x in np.arange(0,11,1)])
See that you need 11 instead of 10 just to add the last tick, you could also do it with linspace.
CodePudding user response:
You were almost right, you have to set major formatter to ScalarFormatter
before using ax.ticklabel_format(style='plain')
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
months = ["January",
"February",
"March",
"April"
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"]
df = pd.DataFrame({
'month': months,
'monthly income': [i*10**6 for i in range(len(months)-1, -1, -1)],
'monthly expenses': [i*.9*10**6 for i in range(len(months)-1, -1, -1)]
})
df['monthly savings'] = df['monthly income'] - df['monthly expenses']
fig, ax = plt.subplots(figsize=(15, 4))
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.ticklabel_format(style='plain')
legend_labels = df.columns[1:]
for label in legend_labels:
ax.plot(df['month'], df[label])
ax.set_ylabel("Dollar")
ax.set_xlabel("Month")
ax.legend(legend_labels)