如何在Python中以数学形式更改$$中的字体 [英] How to change font in $$ which is in math form in python

查看:272
本文介绍了如何在Python中以数学形式更改$$中的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用matplotlib插入图形时,我同时在x轴标签上写了文本和数学文本".因为我必须在标签中写一个化学式,所以它写为"$ CO_2 $浓度".问题是我希望字体应该是新的罗马字体,但是我不能以某种方式更改美元符号中的字体.有谁可以帮助我修复此问题?非常感谢你!

When I tried to polt a figure with matplotlib, I wrote the x axis label with both text and "math text". Because I have to write a chemical formula in the label, it was written as '$CO_2$ concentration'. The question is that I hope the font should be times new roman, but I cannot change the font in the dollar sign somehow. Is there anyone who can help me fix it? Thank you very much!

import numpy as np 
import matplotlib.pyplot as plt
import pandas as pd

xf1 = pd.read_excel('1812_GPT.xlsx',sheetname= 'PVD_CO2CH4_600',usecols=[1])
deltapx1 = pd.read_excel('1812_GPT.xlsx',sheetname= 'PVD_CO2CH4_600',usecols=[3])
Px1 = pd.read_excel('1812_GPT.xlsx',sheetname= 'PVD_CO2CH4_600',usecols=[5])

ax1 = plt.subplot(111) 

l1, = ax1.plot(xf1,Px1,'s',markerfacecolor='black')

font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 14,
 }

ax1.set_xlabel(r'$CO_2$ pressure', font1)
ax1.set_ylabel(r'$CO_2$ concentration', font1)

plt.show()

这是图片链接,您可能会看到图片,发现"Times new roman"中没有"CO2". https://flic.kr/p/2dmh8pj

This is the picture link, you may see the pic and find that "CO2" is not in Times new roman. https://flic.kr/p/2dmh8pj

推荐答案

我认为很难将任何Mathtext更改为任意字体.但是,如果"CO_2"仅包含常规符号,则可以使用\mathdefault{}使数学文本使用常规字体中的符号.

I don't think it's easily possible to change any mathtext to an arbitrary font. However, in case of "CO_2" that consists only of regular symbols, you may use \mathdefault{} to make the mathtext use the symbols from the regular font.

import matplotlib.pyplot as plt

plt.rcParams["font.family"] = "serif"
plt.rcParams["font.serif"] = ["Times New Roman"] + plt.rcParams["font.serif"]

fig, ax = plt.subplots()

ax.set_xlabel(r'$\mathdefault{CO_2}$ pressure')
ax.set_ylabel(r'$\mathdefault{CO_2}$ concentration')

plt.show()

r"$\mathdefault{\sum_\alpha^\beta\frac{i}{9}}$这样的东西仍将以通常的默认数学字体集呈现("i"9除外,它们当然在Times New Roman中存在).

Something like r"$\mathdefault{\sum_\alpha^\beta\frac{i}{9}}$ would still be rendered in the usual default math fontset (except the "i" and the 9, which are of course present in Times New Roman).

对于一般情况,您还可以将完整的数学字体集更改为任何可用的cmstixstixsansdejavuserifdejavusans.最接近"Times New Roman"的时间是stix.

For the general case, you may also change the complete math fontset to any of the available, cm, stix, stixsans, dejavuserif, dejavusans. The closest to "Times New Roman" would be stix.

import matplotlib.pyplot as plt

rc = {"font.family" : "serif", 
      "mathtext.fontset" : "stix"}
plt.rcParams.update(rc)
plt.rcParams["font.serif"] = ["Times New Roman"] + plt.rcParams["font.serif"]


fig, ax = plt.subplots()

ax.set_xlabel(r'$CO_2$ pressure')
ax.set_ylabel(r'$CO_2$ concentration')

plt.show()

阅读的一般建议是 MathText教程.

这篇关于如何在Python中以数学形式更改$$中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆