Python matplotlib can’t show plot in NON-NixOS

I’m new to nix and I’m using it in manjaro. So, as part of my learning I wanted to plot a simple random matrix using matplotlib

Here is the python code

import numpy as np
import matplotlib.pyplot as plt

# Create a matrix
matrix = np.random.rand(10, 10)

# Show the matrix
plt.imshow(matrix, cmap='viridis')
plt.colorbar()
plt.show()

and here is my flake.nix

{
  description = "flake config for my prototype";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    nixgl.url = "github:nix-community/nixgl";
    flake-utils.url = "github:numtide/flake-utils";
  };

    outputs = { self, nixpkgs, flake-utils , nixgl }:
        let
            system = "x86_64-linux";
            pkgs = import nixpkgs {·
                    inherit system;·
                    overlays = [ nixgl.overlay ];
            };
            python = pkgs.python311;
            arduino-cli = pkgs.arduino-cli;
        in
        {
            devShells.${system}.default = pkgs.mkShell {
                buildInputs = [·
                    pkgs.nixgl.nixGLIntel
                    python·
                    python.pkgs.pyserial
                    python.pkgs.numpy
                    python.pkgs.matplotlib
                    arduino-cli·
                ];

                shellHook = ''
                    echo "Hello from the devShell"
                '';
            };

        };
}

How I run the script

$ nix develop -i
$ python test.py

There is no output and no errors. nothing. my workaround is saving the plot into an image, but I have some projects that requires to display a plot dynamically, so I really need that functionality.

I tried the solution proposed in this other post, but I didn’t help. I would appreciate some tips. thanks

I’m not sure about the nixgl thing there, but I’d try instead:

(python.withPackages(ps: [
    ps.pyserial
    ps.numpy
    ps.matplotlib
]))

I don’t think OpenGL has anything to do with your problem. This is likely Qt applications being non-functional by default unless their executable are wrapped by a script that sets up a bunch of environment variables (which is impossible for python):

Try using another backend (TkAgg, for one, works fine) or add this to your mkShell call:

    QT_PLUGIN_PATH = with pkgs.qt5; "${qtbase}/${qtbase.qtPluginPrefix}";
1 Like

I worked using only the libraries suggested by @doronbehar