17

The code below works.

names = years 1960 - 2016

values = GDP of the US for each year

The code produces two charts, but the y-axis label runs from 5000 to 175000. I want to 1) format the label with a "," for a thousands separator, so as an example, 5,000 or 17,500, and,

2) I want to increase the font size of the labels - so increase the font of, for example, 5000.

Not finding a workable/understandable example on line. Help appreciated.

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
from numpy import array

# Plot GDP/Year

names =  usa.loc[: , "Year"]
values = usa.loc[: , "GDP Billions"]

plt.figure(1, figsize=(15, 6))
plt.suptitle('GDP Growth', fontsize=20)

plt.subplot(121)
plt.plot(names, values)
plt.xticks(np.arange(0, 57, step=5.0))
plt.ylabel('GDP', fontsize=16)
plt.title('United States',fontsize=16)

plt.subplot(122)
plt.plot(names, values)
plt.xticks(np.arange(0, 57, step=5.0))
plt.xlabel('Year', fontsize=16)
plt.title('United States',fontsize=16)

#plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 4))
#print(plt.xticks())


plt.show()
user3504322
  • 545
  • 2
  • 5
  • 18
  • 1
    To add the commas: https://stackoverflow.com/questions/25973581/how-do-i-format-axis-number-format-to-thousands-with-a-comma-in-matplotlib To increase the font size: https://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot – Easton Bornemeier Aug 07 '18 at 19:41
  • I saw that one, but couldn't get it too work. I did get the font size issue foxed with the code: plt.yticks(fontsize=12) – user3504322 Aug 07 '18 at 19:44

1 Answers1

35

1) format the label with a "," for a thousands separator, so as an example, 5,000 or 17,500, and (as in How do I format axis number format to thousands with a comma in matplotlib?)

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

2) I want to increase the font size of the labels - so increase the font of, for example, 5000:

plt.rc('ytick', labelsize=5000) 

Here's how you change your code to incorporate these solutions (as requested in the comment):

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
from numpy import array

plt.rc('ytick', labelsize=12) 

# Plot GDP/Year
names =  usa.loc[: , "Year"]
values = usa.loc[: , "GDP Billions"]


fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))

fig.suptitle('GDP Growth', fontsize=20)

ax1.plot(names, values)
ax1.set_xticklabels(np.arange(0, 57, step=5.0))
ax1.set_ylabel('GDP', fontsize=16)
ax1.set_title('United States',fontsize=16)
ax1.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

ax2.plot(names, values)
ax2.set_xticklabels(np.arange(0, 57, step=5.0))
ax2.set_ylabel('Year', fontsize=16)
ax2.set_title('United States',fontsize=16)
ax2.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
#plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 4))
#print(plt.xticks())


plt.show()

And here's what the plots look like for a dummy data I created: enter image description here

Peybae
  • 1,093
  • 14
  • 25
  • Thx for this, but no joy. Probably because I have no clue what to do it it. If I simply cut and paste and execute, I get the error: TypeError: 'AxesSubplot' object is not iterable. The syntax ax.get. . . is not defined in my code. I don't know how to include it. How do I access the get_xaxis().set_major_formatter() method from my code? – user3504322 Aug 07 '18 at 19:55
  • plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 4)) seems promising, but can't figure out how to get the comma separator for thousands. – user3504322 Aug 07 '18 at 20:00
  • @user3504322 look at my updated answer. All you need to change in your code is to define your two plots as `fig, (ax1, ax2) = plt.subplots(1, 2)` and you will have access to `get_xaxis().set_major_formatter()` method through `ax1` and `ax2` for each of your plots – Peybae Aug 07 '18 at 20:09
  • 1
    I don't know how to do that. I am ignorant. – user3504322 Aug 07 '18 at 20:57
  • check my re-updated answer!! I added changes you need to incorporate in your code! – Peybae Aug 07 '18 at 21:17
  • For this code to currently work you need `import matplotlib` and not just `import matplotlib.pyplot as plt` otherwise you get an error – mrp Dec 18 '22 at 20:30
  • Using `'.'` format as separator cause an error – Muhammad Yasirroni Jan 15 '23 at 05:10