Mac Matplotlib 中午字体
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
Python导出eps图
解决
原效果:
plt.ylabel('PR_value',fontproperties='Times New Roman',fontsize=14)
plt.xlabel('Date',fontproperties='Times New Roman',fontsize=14)
plt.xticks(fontproperties='Times New Roman',fontsize=12)
plt.yticks(fontproperties='Times New Roman',fontsize=12)
将字体设置为罗马字体
效果如下:
xtick
plt.xticks(fontproperties='Times New Roman',fontsize=14,rotation='vertical')
Matplotlib
plt.savefig(r'C:\Users\Lenovo\Desktop\Files_dataminging\20190607\The_PR_value_of_the_Tianhe_Sports_Center.png', format='png')
plt.savefig(r'C:\Users\Lenovo\Desktop\Files_dataminging\20190607\The_PR_value_of_the_Tianhe_Sports_Center.eps', format='eps')
正如大家所见,保存下来的图片显示不完整。
解决图片保存不完整
于是,去到:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html#matplotlib.pyplot.savefig
查看相关参数设置:
plt.savefig(r'C:\Users\Lenovo\Desktop\Files_dataminging\20190611\name.eps', format='eps', bbox_inches='tight')
于是解决。
在程序里面使用pyplot.savefig(name,format=“eps”),可以用format指定格式为eps,也可以不使用format,而是写在name里面,它会自动从name里面找到扩展名来决定格式。
虽然这样得到了.eps格式的图像,但是它的周围有空白的地方,有两种方法来去掉这些空白,一种方法是使用gsview,在windows上,一般会使用这个工具,另一种方法是使用epstool(http://pages.cs.wisc.edu/~ghost/gsview/epstool.htm),我使用的代码是
print target
tp0 = "tp0" + target
tp1 = "tp1" + target
plt.savefig(tp0,dpi=160)
cmd0 = "epstool -b -t4 --output %s %s" %(tp1,tp0)
cmd1 = "epstool -p --output %s %s" %(target,tp1)
cmd2 = "rm tp*"
os.system(cmd0)
os.system(cmd1)
os.system(cmd2)
一般情况下,输出的eps文件都可以用的,但有时候却会出现问题,比如在latex编译得到的pdf文件里图像只有一部分,或者全是空白等,这主要问题是eps格式里面的boudningbox有负值,这是一个比较麻烦的问题,目前还没有找到好的解决办法,虽然epstool说是可以调整,重新计算boundingbox,但是依然会有负值,暂时的解决办法就是将图片缩小,比如以前是figsize=(16,8),缩小为figsize=(9,4.5)。目前测试的结果就是宽度超过10,boundingbox就会出现负值,而比9大一点还是可以的,一般取9就可以了,缩小的时候,字体也要相应缩小,不然使用默认字体,就出出现字相对图像太大的情况。
之前提到了,可以设置fig的boundingbox,但没有深入研究,有兴趣的可以继续探讨。
作图显示中文
解决matplotlib作图时默认设置下无法显示中文的问题:
添加以下代码及解决
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文
mpl.rcParams['axes.unicode_minus'] = False # 正常显示负号
保存图片模糊问题
plt.savefig(r'C:\Users\Lenovo\Desktop\Files_dataminging\20190611\%s.jpg' % name, format='jpg', bbox_inches='tight', dpi=300)
指定分辨率保存
效果:
linestyle 点、线
import matplotlib.pylab as plt
markers=['.',',','o','v','^','<','>','1','2','3','4','8','s','p','P','*','h','H','+','x','X','D','d','|','_']
descriptions=['point', 'pixel', 'circle', 'triangle_down', 'triangle_up','triangle_left', 'triangle_right', 'tri_down', 'tri_up', 'tri_left','tri_right', 'octagon', 'square', 'pentagon', 'plus (filled)','star', 'hexagon1', 'hexagon2', 'plus', 'x', 'x (filled)','diamond', 'thin_diamond', 'vline', 'hline']
x=[]
y=[]
for i in range(5):
for j in range(5):
x.append(i)
y.append(j)
plt.figure()
for i,j,m,l in zip(x,y,markers,descriptions):
plt.scatter(i,j,marker=m)
plt.text(i-0.15,j+0.15,s=m+' : '+l)
plt.axis([-0.1,4.8,-0.1,4.5])
plt.tight_layout()
plt.axis('off')
plt.show()
至此,对Matplotlib的介绍就结束了,以后或许会用到新的东西,到时候在进行介绍。