Sankey图matplotlib保存比例问题

时间:2017-05-01 12:00:24

标签: python matplotlib scale sankey-diagram

使用matplotlib从维基百科生成数据的Sankey图表(看起来非常糟糕的数据,但我想出了代码,然后寻找更好的数据)。我似乎无法正确扩展文件。

在macOS 10.12.4的系统plt.show()中显示Python.app,并且简单示例保存 fine ,但添加{ {1}}似乎以某种方式摧毁了scale = 0.0001。我已尝试更改plt.savefig()参数,但它似乎根本不会影响结果。我还尝试向dpi添加scale参数,但这似乎根本没有做任何事情(甚至没有打破它?)。

我可以从savefig保存它看起来很好,但我真的想弄清楚出了什么问题。

Python.app

import matplotlib.pyplot as plt from matplotlib.sankey import Sankey fig = plt.figure(figsize = (13, 7), frameon = False) ax = fig.add_subplot(1, 1, 1, xticks = [], yticks = [], title='Global Electricity Production & Consumption 2005' ) g = [-12192, 6157, 1960, 387, 2383, 1240] # generated c = [4250, -7942, -1418, -1266, -1017, -8.79] # consumed sankey = Sankey(ax = ax, format = '%.5G', head_angle = 135, unit = ' TWh', gap = 0.3, scale = 0.0001, margin = 0.0, offset = 0.2, shoulder = 0.0) sankey.add( patchlabel = 'Production', flows = g, orientations = [0, 0, -1, -1, 1, 1], labels = [None, 'Coal', 'Natural Gas', 'Petroleum', 'Nuclear', 'Renewable'], pathlengths = [0.0, 0.2, 0.2, 0.6, 0.2, 0.2] ) sankey.add( flows = [12192, -4250, -7942], orientations = [0, 0, -1], labels = [None, None, 'Conversion Losses'], pathlengths = [-.2, -.2, 0.4], # trunklength = 1.0, prior = 0, connect = (0, 0)) # denotes which flow index from the prior to connect to which flow index in this one sankey.add( patchlabel = 'Gross Generation\n4250 TWh', flows = [4250, -1418, -1266, -1017, -8.79, -541], orientations = [0, 0, -1, 1, -1, 1], labels = [None, 'Residential', 'Commercial', 'Industrial', 'Transportation', '?'], prior = 1, pathlengths = [0.2, 0.2, 0.2, 0.2, 0.7, 0.2], # trunklength = 2.5, connect = (1, 0) ) plt.savefig('./Global_Electrical_Energy_Prod_Cons_2005.png', dpi = 300, frameon = None, transparent = True, scale = 0.0001) sankey.finish() plt.show() 启动的Python.app保存的预期结果。

The expected result (saved from Python.app)

令人困惑的结果(编辑:这是乱码但是因为它仍设置为300 dpi,使用默认的let stackoverflow正确显示它)。我刚试过plt.show().pdf并得到了相同的结果。

The result of the save line

1 个答案:

答案 0 :(得分:2)

您需要在保存前完成sankey图。更改这些行的顺序:

sankey.finish()
plt.savefig(...)