生成pdf文件时的Matplotlib和Tex问题

时间:2018-09-20 08:10:53

标签: python-2.7 matplotlib tex

我正在尝试使用matplotlib绘制一组图形。我得到的输出不是我期望的。我正在尝试找到一种解决方法。我的问题是

1)该图在X标题中的#前面打印\字符。如果我省略'\',则会收到一条错误消息:“您不能在垂直模式下使用'宏参数字符#'”

2)刻度标签的格式不统一。左上方的标签为tex格式,与Y轴的间距很好,而左下方的标签未为tex格式,而刻度线标签则与Y轴接触。如何获得底部标签的行为与顶部标签的行为相同?

3)最后,如何在创建的pdf中仅获取Type 1字体?现在,如下所示

姓名类型


DejaVuSans Type 3
CMSS12类型1
CMSS10类型1

我的代码如下。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc

filename = 'eg.pdf'

fig, ax1 = plt.subplots(frameon=False)
rc('mathtext', default='regular')
rc('lines',lw=1.6)
rc('lines',mew=1.4)
rc('text', usetex=True)
x = np.array([10,20,30,40])

risk_ndcg = np.array([2.44,3.46,4.16,5.84])
sqrt_ndcg = np.array([2.30,3.05,4.56,5.20])
mod_ndcg  = np.array([2.66,3.43,4.91,5.49])
mf_ndcg   = np.array([2.20,3.55,4.04,5.37])

lns1 = ax1.plot(x,sqrt_ndcg,':', markersize=3.5, label='XX');
lns2 = ax1.plot(x,mod_ndcg,'-.', markersize=3.5, label='XY');
lns3 = ax1.plot(x,mf_ndcg,'--',markersize=3.5, label='XZ');
lns4 = ax1.plot(x,risk_ndcg,'-',markersize=3.5, label='ZZ');

ax1.set_ylabel(r'ACC',size=13);
ax1.set_ylim([2,6]);
ax1.set_xlim([0,42]);
ax1.set_xticks(x);
ax1.tick_params(axis='y', which=u'both', length=0, labelsize=12);
ax1.tick_params(axis='x', which=u'both', length=0, labelsize=12);

lns = lns1 + lns2 + lns3 + lns4;
labs = [l.get_label() for l in lns];

ax1.set_xlabel(r'\# of points',size=13);
lgd = ax1.legend(lns, labs, bbox_to_anchor=(1.0,0.5),loc=0, ncol = 1, fontsize='11',shadow=True,fancybox=True);
fig.savefig(filename,format='pdf',transparent=True, bbox_extra_artists=(lgd,), bbox_inches='tight');

My current output

1 个答案:

答案 0 :(得分:0)

这很奇怪,因为我没有与您的问题相同的问题。 这是我的输出:https://ufile.io/b34e4

我想这就是你想要的。

它与Python 2.7和matplotlib 1.5.1一起运行。 什么是配置?