Skip to content

Allow selection of the Matplotlib backend using the MPLBACKEND enviro… - #135

Closed
jheinen wants to merge 3 commits into
JuliaPy:masterfrom
jheinen:master
Closed

jheinen wants to merge 3 commits into
JuliaPy:masterfrom
jheinen:master

Conversation

@jheinen

@jheinen jheinen commented Jun 11, 2015

Copy link
Copy Markdown
Contributor

This patch allows setting the Matplotlib backend without changing the users' scripts or customizing default options (in .matplotlibrc). This feature is especially useful when mixing (existing) PyPlot calls with functions provided by the GR framework, a universal framework for cross-platform visualization applications.

A similar pull request (for Matplotlib) has already been accepted by the MPL developers. Here you can find a Python example which can easily be ported to Julia, providing enhanced features, especially for real-time visualization.

This is how a Julia example would look like:

import PyCall
@PyCall.pyimport numpy as np

import PyPlot
mpl = PyPlot

import GR

angles = np.load("700K_460.npy")
lens = []

for t in 1:100 
    mpl.cla()
    fig = mpl.subplot(211)
    mpl.ylim([0, 1000])
    mpl.hist(angles[t], 20, normed=0, facecolor="g", alpha=0.5)
    mpl.show()

    lens = [lens, np.size(angles[t])]
    if t > 1
        GR.setwindow(0, 10, 3500, 5000)
        GR.setviewport(0.1, 0.9, 0.05, 0.4)
        GR.axes(1, 0, 0, 3500, 2, 0, 0.005)
        GR.polyline(np.arange(t) / 10.0, lens)
    end

    GR.updatews()
end

@stevengj

Copy link
Copy Markdown
Member

This is the wrong place to put it — you need to modify the find_backend function so that you initialize the desired backend.

@jheinen

jheinen commented Jun 13, 2015

Copy link
Copy Markdown
Contributor Author

I understand. I changed the logic in the last commit (0321c0d). The find_backend function is no longer called when the MPLBACKEND environment is set. The user should be able to switch the backend without changing a single line of code. Is this ok?

@stevengj

stevengj commented Jul 2, 2015

Copy link
Copy Markdown
Member

No, you still need find_backend. For one thing, the MPLBACKEND might not be supported. For another thing, find_backend figures out whether we need to start a GUI event-loop thread for the backend.

Basically, I think you should just alter/extend the default = lowercase(get(rcParams, "backend", "none")) line in find_backend

Question: if you have both MPLBACKEND and a backend specified in your matplotlibrc, which takes precedence?

@stevengj

Copy link
Copy Markdown
Member

Ah, I see the docs say: Setting this environment variable will override the``backend``parameter in *any*``matplotlibrc``, even if there is a``matplotlibrc``in your current working directory.

@stevengj stevengj closed this in 70ea59b Jul 27, 2015
@stevengj

Copy link
Copy Markdown
Member

Implemented in 70ea59b

@jheinen

jheinen commented Jul 28, 2015

Copy link
Copy Markdown
Contributor Author

Checked out PyPlot.jl master branch - works perfect.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants