Last active
July 16, 2018 01:32
-
-
Save pluswave/275d9d965aa9159f134339452d70e002 to your computer and use it in GitHub Desktop.
基于matplotlib输出心电图纸到pdf文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#*-- coding: utf-8 --* | |
import matplotlib | |
X_INCH = 7 | |
Y_INCH = 11 | |
X_MM = int(X_INCH*25.4) | |
Y_MM = int(Y_INCH*25.4) | |
while X_MM % 5 != 1: | |
X_MM -= 1 | |
while Y_MM % 5 != 1: | |
Y_MM -= 1 | |
M_X_INCH = float(X_MM) / 25.4 | |
M_Y_INCH = float(Y_MM) / 25.4 | |
THIN_WIDTH = 0.2 | |
FAT_WIDTH = 0.6 | |
matplotlib.use('pdf') | |
from matplotlib.backends.backend_pdf import PdfPages | |
import matplotlib.pyplot as plt | |
with PdfPages('ecg.pdf') as pdf: | |
f = plt.figure(figsize=(M_X_INCH,M_Y_INCH), dpi=600) # 英寸 | |
plt.rc('lines', linewidth=THIN_WIDTH, color='hotpink') | |
# 坐标系 | |
# axes = f.add_subplot(1,1,1) | |
axes = f.add_axes( (0, 0, 1, 1), frame_on=False ) | |
axes.set_xlim(-0.5, X_MM-0.5) # 毫米 | |
axes.set_ylim(-0.5, Y_MM-0.5) | |
def get_line_width(i): | |
if i % 5 == 0 : | |
return FAT_WIDTH | |
else: | |
return THIN_WIDTH | |
for i in range(0, X_MM): | |
axes.axvline(x=i,linewidth=get_line_width(i)) | |
for i in range(0, Y_MM): | |
axes.axhline(y=i,linewidth=get_line_width(i)) | |
pdf.savefig() | |
plt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
windows 上有报错的,注释这行解决了我的错误
#matplotlib.use('pdf')