以某个dpi保存图表

时间:2017-07-25 19:16:31

标签: python-2.7 matplotlib

我有以下图表:

RuntimeforRecords = {10: 0.164, 20: 0.057, 30: 0.065, 40: 0.061, 50: 0.097, 60: 0.099, 70: 0.103, 80: 0.162, 90: 0.217, 100: 0.2,
 110: 0.19, 120: 0.155, 130: 0.172, 140: 0.167, 150: 0.174, 160: 0.195, 170: 0.203, 180: 0.228, 190: 0.244, 200: 0.235,
 210: 0.253, 220: 0.274, 230: 0.282, 240: 0.288, 250: 0.426, 260: 0.345, 270: 0.333, 280: 0.345, 290: 0.387, 300: 0.346,
 310: 0.401, 320: 0.401, 330: 0.406, 340: 0.451, 350: 0.549, 360: 0.465, 370: 0.474, 380: 0.483, 390: 0.457, 400: 0.551,
 410: 0.536, 420: 0.54, 430: 0.565, 440: 0.568, 450: 0.563, 460: 0.643, 470: 0.614, 480: 0.649, 490: 0.655, 500: 0.692,
 510: 0.703, 520: 0.727, 530: 0.74, 540: 0.759, 550: 0.744, 560: 0.738, 570: 0.77, 580: 0.801, 590: 0.802, 600: 0.818,
 610: 0.791, 620: 0.868, 630: 0.893, 640: 0.911, 650: 0.947, 660: 0.926, 670: 1.001, 680: 0.957, 690: 0.98, 700: 1.035,
 710: 1.057, 720: 1.004, 730: 1.112, 740: 1.011, 750: 1.13, 760: 1.119, 770: 1.123, 780: 1.112, 790: 1.204, 800: 1.155,
 810: 1.356, 820: 1.309, 830: 1.214, 840: 1.334, 850: 1.345, 860: 1.327, 870: 1.34, 880: 1.382, 890: 1.349, 900: 1.43,
 910: 1.352, 920: 1.413, 930: 1.458, 940: 1.409, 950: 1.419, 960: 1.55, 970: 1.433, 980: 1.482, 990: 1.524, 1000: 1.585,
 1100: 1.781, 1200: 1.861, 1300: 2.052, 1400: 2.462, 1500: 2.499, 1600: 2.809, 1700: 3.006, 1800: 3.086, 1900: 3.863,
 2000: 4.067, 2100: 3.899, 2200: 4.552, 2300: 4.855, 2400: 5.002, 2500: 5.527, 2600: 5.992, 2700: 6.111, 2800: 6.332,
 2900: 6.785, 3000: 6.42, 3100: 6.836, 3200: 7.88, 3300: 9.157, 3400: 9.146, 3500: 9.661, 3600: 13.574, 3700: 14.292,
 3800: 12.224, 3900: 9.622, 4000: 17.853, 4100: 23.878, 4200: 20.01, 4300: 12.269, 4400: 29.728, 4500: 14.957,
 4600: 13.673, 4700: 13.555, 4800: 28.883, 4900: 35.344, 5000: 16.899, 5100: 36.355, 5200: 17.796, 5300: 37.356,
 5400: 37.234, 5500: 41.227, 5600: 19.204, 5700: 35.477, 5800: 49.428, 5900: 27.822, 6000: 65.389, 6100: 38.761,
 6200: 42.774, 6300: 60.65, 6400: 42.57, 6500: 30.596, 6600: 23.748, 6700: 32.562, 6800: 55.423, 6900: 43.923,
 7000: 57.201, 7100: 54.869, 7200: 31.913, 7300: 66.946, 7400: 77.523, 7500: 46.84, 7600: 61.002, 7700: 65.134,
 7800: 46.37, 7900: 82.23, 8000: 40.585, 8100: 39.185, 8200: 95.613, 8300: 65.256, 8400: 58.655, 8500: 53.839,
 8600: 78.684, 8700: 101.803, 8800: 87.968, 8900: 75.537, 9000: 39.714, 9100: 83.88, 9200: 147.055, 9300: 115.031,
 9400: 137.924, 9500: 73.337, 9600: 126.971, 9700: 69.119, 9800: 123.658, 9900: 163.08, 10000: 111.361}



from scipy.interpolate import spline
import matplotlib.pyplot as plt
import numpy as np
import collections
Runtimeadj = collections.OrderedDict(sorted(RuntimeforRecords.items()))

P = np.array(Runtimeadj.values())
T = np.array(Runtimeadj.keys())

xnew = np.linspace(T.min(),T.max(),300) #300 represents number of points to make between T.min and T.max

P_smooth = spline(T,P,xnew)

plt.plot(xnew,P_smooth,color='k')
plt.xlabel('Number of Employees')
plt.ylabel('Run Time (s)')
plt.show()

正如您从主题中可以理解的那样,我试图用png格式的1200 dpi保存它。使用以下代码:

fig, ax = plt.subplots()
# Do the plot code
fig.savefig('myimage.png', format='png', dpi=1200)

我不知道我在哪里做错了,但是,它只是保存了没有曲线的框架。如果有人可以更正代码,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

你创建了一个ax但你没有插入它,试试这样:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(xnew,P_smooth,color='k')
ax.set_xlabel('Number of Employees')
ax.set_ylabel('Run Time (s)')
plt.savefig('myimage.png', format='png', dpi=1200)