英文:
How to have a space thousand separator on pyplot graphs
问题
我正在绘制图形,但我的y轴值最高达到175,000。当然,Python和Pyplot默认情况下不使用千位分隔符。这些图形必须符合格式规则的文章,因此我正在使用rc库以使用衬线字体并启用Latex。然而,规则还要求使用空格千位分隔符。我正在从数据框中绘制我的图形,并找到了一些答案,可以在整个数据框中包括逗号千位分隔符。然后,我可以使用.replace()
将逗号替换为空格。然而,问题在于,当我的数据框中有任何千位分隔符时,我的图形就无法显示。内核只是不断运行,没有任何图形输出。
所以我的问题是是否有一种方法可以引入空格千位分隔符并使用Pyplot进行绘制?最好是一两行代码,我可以将其应用于整个数据框,而不必逐列应用,但任何解决方案都将不胜感激。
以下是我的代码:
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
这是用于在整个数据框中获取逗号分隔符的代码,但无法绘制图形:
data = pd.read_excel(r"C:\Users\pivde\Desktop\Tuks\nagraads\karakterisering\xrd\sifeksp_filament.xlsx", sheet_name = "python_data")
data = data.applymap(lambda x: f'{x:,d}' if isinstance(x, int) else x)
然而,如果我改为使用以下代码:
data = pd.read_excel(r"C:\Users\pivde\Desktop\Tuks\nagraads\karakterisering\xrd\sifeksp_filament.xlsx", sheet_name = "python_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)
我无法附加包含数据的Excel文件,所以我附上了一个缩小的数据集以便您参考,其中包含足够的数据来说明问题。
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]
英文:
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:\Users\pivde\Desktop\Tuks\nagraads\karakterisering\xrd\sifeksp_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:\Users\pivde\Desktop\Tuks\nagraads\karakterisering\xrd\sifeksp_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]
答案1
得分: 1
以下是要翻译的代码部分:
使用`format()`函数添加`,`,然后使用`replace`将其更改为空格是一种方法。我已经在下面的代码中完成了这个操作,它将独立运行。希望这是您要寻找的内容...
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)
输出图表
[![enter image description here][1]][1]
请注意,我已将HTML实体(如`"`)更改为其对应的字符。
<details>
<summary>英文:</summary>
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, 5sketsverhouding
half = 3, 3sketsverhouding
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][1]][1]
[1]: https://i.stack.imgur.com/wTtHE.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论