Jim*_*lum 3 python pdf charts pdf-generation matplotlib
我正在使用官方 matplotlib 页面http://matplotlib.org/examples/api/barchart_demo.html上存在的示例代码
#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这个情节,显示得绝对正确。但是,如果我想保存它,请在代码底部使用以下命令,它不会保存绘图。
plt.savefig("result.pdf")
Run Code Online (Sandbox Code Playgroud)
难道是我做错了什么?
移动plt.savefig("result.pdf")之前的线plt.show()应将绘图正确保存为 pdf 文件。
您可能正在使用交互式后端。之后plt.show(),会弹出一个包含绘图的窗口。如果随后关闭窗口,绘图就会消失(类似于通话plt.close())。因此,plt.savefig()如果您在关闭绘图窗口后执行此操作,则不会保存任何内容。
| 归档时间: |
|
| 查看次数: |
6000 次 |
| 最近记录: |