1

Using matplotlib to generate a Sankey diagram of data from wikipedia (looks like really bad data but figured I'd figure out the code and then go looking for better data). And I can't seem to get the file to scale correctly.

It displays fine with plt.show() in the system Python.app in macOS 10.12.4, and the simple examples saved fine, but adding scale = 0.0001 seems to have destroyed the plt.savefig() somehow. I’ve tried changing the dpi parameter but it doesn't seem to affect the result at all. I also tried adding a scale parameter to savefig but that didn‘t seem to do anything at all (not even break it?).

I can save it from Python.app and it looks fine, but I really wanted to figure out what is going wrong.

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()

The expected result saved from Python.app launched by plt.show().

The expected result (saved from Python.app)

The baffling result (edit: this was scrambled but it was because it was set to 300 dpi still, using the default let stackoverflow display it properly). I just tried .pdf and .svg and got the same results.

The result of the save line

Cody
  • 721
  • 7
  • 15
  • 3
    maybe try saving after calling `sankey.finish()`? – gereleth May 01 '17 at 13:00
  • Great idea! I wish I had thought to try it, but looks like it didn't work either, same result. – Cody May 01 '17 at 13:02
  • 3
    Are you sure? That does fix it when I run the code... – Paul Brodersen May 01 '17 at 13:10
  • Actually, no, I'm not sure! I just realized I edited it in the wrong file (I copied the code over to start adding new data, but still ran the original file) I can't test it at the moment cause my computer just froze from a memory issue, but I should be able to check it in a minute — sounds promising, thanks! – Cody May 01 '17 at 13:13
  • 1
    You indeed need to finish your sankey plot prior to saving. Just to mention, `savefig` does not have a `scale` argument, so best leave that out. – ImportanceOfBeingErnest May 01 '17 at 13:13
  • @gereleth yup that worked! Thank you! Want to make it an answer so I can accept it? – Cody May 01 '17 at 13:27

1 Answers1

2

You need to finish the sankey plot before saving it. Change the order of these lines:

sankey.finish()
plt.savefig(...)
gereleth
  • 2,452
  • 12
  • 21