How to have a space thousand separator on pyplot graphs

Question:

I am plotting graphs but the values on my y-axis goes up to 175 000. Of course, python and pyplot does not use thousand separators by default. The graphs have to go into an article subject to formatting rules, and therefore I am using the rc library to have a serif font and to enable the use of latex. The rules however also require a space thousand separator. I am plotting my graphs from a data frame, and have found answers where I can include a comma thousand separator in my whole data frame. I can then use .replace() to replace the comma with a space. The problem is however that when I have either thousand separator in my data frame, my graphs won’t show. The kernel just keeps on running without any graph as output.

So my question is whether there is a way to introduce the space thousand separator and plot it with pyplot? Preferably this would be one or two lines of code that I can apply to the whole data frame instead of having to apply it to one column at a time, but any solution would be appreciated.

Attached is my code.

import numpy as np
import pandas as pd
from matplotlib import pyplot as pp
%matplotlib inline
from matplotlib import rc
rc("text", usetex=True)
rc("font", family="serif")
sketsverhouding = 4 / 5
vol = 5, 5*sketsverhouding
half = 3, 3*sketsverhouding
derde = 2.3, 2.3*sketsverhouding

Here is the code used to get the comma separator in the whole data frame, which cannot be graphed.

data = pd.read_excel(r"C:UserspivdeDesktopTuksnagraadskarakteriseringxrdsifeksp_filament.xlsx", sheet_name = "python_data")
data = data.applymap(lambda x: f'{x:,d}' if isinstance(x, int) else x)

If however, I use this instead

data = pd.read_excel(r"C:UserspivdeDesktopTuksnagraadskarakteriseringxrdsifeksp_filament.xlsx", sheet_name = "python_data")

I can plot my graphs normally, but they do not have any thousand separators. My graphing function is shown below with two lines graphing data.

def skets(y, etiket):
    pp.figure(figsize=half)
    pp.plot(data['2theta'], data[y], 'k-')
    pp.xlabel(r'2$theta$ [$^circ$]')
    pp.ylabel('Intensity')
    pp.tight_layout()
    pp.ylim(0, 15000)
    pp.savefig(naam.format(etiket))

naam = 'grafieke/xrd_sifekpsfil_{:03.0f}ldh_pla.svg'
skets('n', 0)
skets('t', 2)

I cannot attach the excel file with the data, so I attach a reduced data set for your convenience with enough data to illustrate the point.

2theta = [4.998436247,10.63245752,20.27627772,30.37691046,40.62981404,50.83196067,61.08486426,70.01808718]
n = [6090,1387,6762,3178,2865,2121,1354,1243]
t = [6146,2266,8610,4012,3424,2390,1572,1355]
Asked By: Philip de Bruin

||

Answers:

One way to do this is to use the format() function to add , and then use replace to change it to space. I have done this in below code, which will run independently. Hope this is what you are looking for…

theta = [4.998436247,10.63245752,20.27627772,30.37691046,40.62981404,50.83196067,61.08486426,70.01808718]
n = [6090,1387,6762,3178,2865,2121,1354,1243]
t = [6146,2266,8610,4012,3424,2390,1572,1355]
import numpy as np
import pandas as pd
from matplotlib import pyplot as pp
%matplotlib inline
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
from matplotlib import rc
#rc("text", usetex=True)
rc("font", family="serif")
sketsverhouding = 4 / 5
vol = 5, 5*sketsverhouding
half = 3, 3*sketsverhouding
derde = 2.3, 2.3*sketsverhouding

def skets(y, etiket):
    pp.figure(figsize=half)
    pp.plot(theta, n, 'k-')
    pp.xlabel(r'2$theta$ [$^circ$]')
    pp.ylabel('Intensity')
    pp.ylim(0, 15000)
    labels = pp.gca().get_yticks()
    pp.gca().get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, p: '{:,}'.format(x).replace(',', ' ')))
    
skets('n', 0)

Output plot

enter image description here

Answered By: Redox