【Python】Matplotlib画布图案保存为PDF文件
生活随笔
收集整理的這篇文章主要介紹了
【Python】Matplotlib画布图案保存为PDF文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原圖Link
這里選擇兩個繪過的圖組合起來,生成兩頁的PDF文件,原圖Link:
- Matplotlib繪制帶顏色標尺的彩色曲面
- Matplotlib繪制三維馬鞍面
Matplotlib編程實現
import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter from mpl_toolkits.mplot3d import Axes3D from matplotlib.backends.backend_pdf import PdfPageswith PdfPages("D://PdfPages.pdf") as pdf:fig = plt.figure()ax = fig.add_subplot(1, 1, 1, projection="3d")x = np.arange(-3, 3, 0.25)y = np.arange(-3, 3, 0.25)x, y = np.meshgrid(x, y)r = np.sqrt(np.power(x, 2) + np.power(y, 2))z = np.sin(r)surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)ax.set(zlim=(-1, 1))ax.zaxis.set_major_locator(LinearLocator(7))ax.zaxis.set_major_formatter(FormatStrFormatter("%3.2f"))fig.colorbar(surf, shrink=0.6, aspect=10)plt.title("Page1")pdf.savefig()plt.close()fig = plt.figure()ax = fig.gca(projection='3d')n_radii = 8n_angles = 36radii = np.linspace(0.125, 1.0, n_radii)angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)x = np.append(0, (radii * np.cos(angles)).flatten())y = np.append(0, (radii * np.sin(angles)).flatten())z = np.sin(-x * y)ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)fig.suptitle("Page2")pdf.savefig()plt.close()找到D盤根路徑,發現出現這個文件
打開文件后
總結
以上是生活随笔為你收集整理的【Python】Matplotlib画布图案保存为PDF文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java】Object基类
- 下一篇: 【项目管理】敏捷团队协议示例