Skip to content

Commit 3e2025d

Browse files
committed
set the default backend when writing to a file to be agg; some platforms screw this up
1 parent fbcc099 commit 3e2025d

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/Graphics/Matplotlib.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ import Graphics.Matplotlib.Internal
7171

7272
-- | Show a plot, blocks until the figure is closed
7373
onscreen :: Matplotlib -> IO (Either String String)
74-
onscreen m = withMplot m (\str -> python $ pyIncludes ++ str ++ pyDetach ++ pyOnscreen)
74+
onscreen m = withMplot m (\s -> python $ pyIncludes "" ++ s ++ pyDetach ++ pyOnscreen)
7575

7676
-- | Print the python code that would be executed
7777
code :: Matplotlib -> IO String
78-
code m = withMplot m (\str -> return $ unlines $ pyIncludes ++ str ++ pyDetach ++ pyOnscreen)
78+
code m = withMplot m (\s -> return $ unlines $ pyIncludes (pyBackend "agg") ++ s ++ pyDetach ++ pyOnscreen)
7979

8080
-- | Save to a file
8181
file :: [Char] -> Matplotlib -> IO (Either String String)
82-
file filename m = withMplot m (\str -> python $ pyIncludes ++ str ++ pyFigure filename)
82+
file filename m = withMplot m (\s -> python $ pyIncludes (pyBackend "agg") ++ s ++ pyFigure filename)
8383

8484
-- * Useful plots
8585

src/Graphics/Matplotlib/Internal.hs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -259,29 +259,30 @@ python codeStr =
259259
Right <$> readProcess "/usr/bin/python3" [codeFile] ""))
260260
(\e -> return $ Left $ show (e :: IOException))
261261

262+
pyBackend backend = "matplotlib.use('" ++ backend ++ "')"
263+
262264
-- | The standard python includes of every plot
263-
pyIncludes :: [[Char]]
264-
pyIncludes = ["import matplotlib"
265-
-- TODO Provide a way to set the render backend
266-
-- ,"matplotlib.use('GtkAgg')"
267-
,"import matplotlib.path as mpath"
268-
,"import matplotlib.patches as mpatches"
269-
,"import matplotlib.pyplot as plot"
270-
,"import matplotlib.mlab as mlab"
271-
,"import matplotlib.colors as mcolors"
272-
,"import matplotlib.collections as mcollections"
273-
,"import matplotlib.ticker as mticker"
274-
,"from matplotlib import cm"
275-
,"from mpl_toolkits.mplot3d import axes3d"
276-
,"import numpy as np"
277-
,"import os"
278-
,"import sys"
279-
,"import json"
280-
,"import random, datetime"
281-
,"from matplotlib.dates import DateFormatter, WeekdayLocator"
282-
,"fig = plot.gcf()"
283-
,"axes = [plot.gca()]"
284-
,"ax = axes[0]"]
265+
pyIncludes :: String -> [[Char]]
266+
pyIncludes backend = ["import matplotlib"
267+
,backend
268+
,"import matplotlib.path as mpath"
269+
,"import matplotlib.patches as mpatches"
270+
,"import matplotlib.pyplot as plot"
271+
,"import matplotlib.mlab as mlab"
272+
,"import matplotlib.colors as mcolors"
273+
,"import matplotlib.collections as mcollections"
274+
,"import matplotlib.ticker as mticker"
275+
,"from matplotlib import cm"
276+
,"from mpl_toolkits.mplot3d import axes3d"
277+
,"import numpy as np"
278+
,"import os"
279+
,"import sys"
280+
,"import json"
281+
,"import random, datetime"
282+
,"from matplotlib.dates import DateFormatter, WeekdayLocator"
283+
,"fig = plot.gcf()"
284+
,"axes = [plot.gca()]"
285+
,"ax = axes[0]"]
285286

286287
-- | The python command that reads external data into the python data array
287288
pyReadData :: [Char] -> [[Char]]

0 commit comments

Comments
 (0)