Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

blank plot with matplotlib inside pipenv on Mac OS X

Short version

Trying to show a plot via plt.show() inside a pipenv opens a window that is completely white, no errors. Resizing the window makes it completely black. Expecting to see a plot depicting a function f(x) = x^2 (see code below).

More info

Saving a plot with plt.savefig('tmp.png') works fine.

Installing matplotlib to the system wide python3 via $ pip3 install matplotlib causes no issues. The issue only arises when matplotlib is installed within a pipenv executing the same code (see below).

Versions:

  • Mac OS Mojave 10.14.2
  • pipenv version 2018.11.26
  • matplotlib==3.0.2
  • pipenv and system python version 3.7.2

Somewhat surprisingly, I can get it to work on an older Mac running Sierra 10.12.6 with otherwise same versions of pipenv, python, and matplotlib. But I don't think I ever ran into the same issue on that one.

What I have tried

  1. installed tcl-tk: $ brew install tcl-tk
  2. set backend: $ echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
  3. installed X11 server

What am I missing?

At this point I am out of ideas. Any hints as to how to get a plot to show up properly within a pipenv is greatly appreciated.

Does this have anything to do with a framework install of python as mentioned in the matplotlib docs?

Code

import numpy as np
import matplotlib
from matplotlib import pyplot as plt

if __name__ == "__main__":
    plt.figure()
    x = np.arange(0, 10, 1)
    y = x*x

    plt.plot(x, y)
    # plt.savefig("tmp.png")    # works fine
    plt.show()
like image 314
pmlk Avatar asked Oct 19 '25 01:10

pmlk


1 Answers

If you're still having this problem, I fixed it by installing python as a framework (I installed conda brew cask install anaconda. Then add your Anaconda to path: export PATH=$HOME/anaconda3/bin/:$PATH via vim ~/.bash_profile. I can still engage pipenv with Anaconda.

like image 85
Rhubarb Avatar answered Oct 21 '25 17:10

Rhubarb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!