{"record":{"id":"768b6aa1887d2614","repo":"Lightning-AI/pytorch-lightning","slug":"to-use-the-plot-method-you-must-have-matplotlib","errorCode":null,"errorMessage":"To use the `plot` method, you must have Matplotlib installed. Install it by running `pip install -U matplotlib`.","messagePattern":"To use the `plot` method, you must have Matplotlib installed\\. Install it by running `pip install -U matplotlib`\\.","errorType":"validation","errorClass":"MisconfigurationException","httpStatus":null,"severity":"warning","filePath":"src/lightning/pytorch/tuner/lr_finder.py","lineNumber":130,"sourceCode":"        args = (optimizer, self.lr_max, self.num_training)\n        scheduler = _LinearLR(*args) if self.mode == \"linear\" else _ExponentialLR(*args)\n\n        trainer.strategy.optimizers = [optimizer]\n        trainer.strategy.lr_scheduler_configs = [LRSchedulerConfig(scheduler, interval=\"step\")]\n        _validate_optimizers_attached(trainer.optimizers, trainer.lr_scheduler_configs)\n\n    def plot(\n        self, suggest: bool = False, show: bool = False, ax: Optional[\"Axes\"] = None\n    ) -> Optional[Union[\"plt.Figure\", \"plt.SubFigure\"]]:\n        \"\"\"Plot results from lr_find run\n        Args:\n            suggest: if True, will mark suggested lr to use with a red point\n            show: if True, will show figure\n            ax: Axes object to which the plot is to be drawn. If not provided, a new figure is created.\n\n        \"\"\"\n        if not _MATPLOTLIB_AVAILABLE:\n            raise MisconfigurationException(\n                \"To use the `plot` method, you must have Matplotlib installed.\"\n                \" Install it by running `pip install -U matplotlib`.\"\n            )\n        import matplotlib.pyplot as plt\n\n        lrs = self.results[\"lr\"]\n        losses = self.results[\"loss\"]\n\n        fig: Optional[Union[plt.Figure, plt.SubFigure]]\n        if ax is None:\n            fig, ax = plt.subplots()\n        else:\n            fig = ax.figure\n\n        # Plot loss as a function of the learning rate\n        ax.plot(lrs, losses)\n        if self.mode == \"exponential\":\n            ax.set_xscale(\"log\")","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/tuner/lr_finder.py#L112-L148","documentation":"_lr_find().plot() requires Matplotlib, but the optional dependency is not installed in the environment (Lightning guards the import with _MATPLOTLIB_AVAILABLE). Plotting is optional so the library raises MisconfigurationException with install instructions instead of an ImportError.","triggerScenarios":"Calling lr_finder.plot() (or trainer.tuner.lr_find(model).plot()) in an environment without matplotlib installed — common in slim Docker/CI images.","commonSituations":"Minimal training-only Docker images, CI pipelines, or cluster environments where matplotlib was never installed because training itself does not need it.","solutions":["pip install -U matplotlib","Skip plot() and use the returned results dict / lr_finder.suggestion() programmatically","If installing is impossible, export self.results['lr'] and self.results['loss'] and plot elsewhere"],"exampleFix":"# before\nlr_finder = tuner.lr_find(model)\nlr_finder.plot()  # MisconfigurationException without matplotlib\n# after (no plotting needed)\nlr_finder = tuner.lr_find(model)\nmodel.hparams.lr = lr_finder.suggestion()","handlingStrategy":"validation","validationCode":"try:\n    import matplotlib  # noqa: F401\n    HAS_MPL = True\nexcept ImportError:\n    HAS_MPL = False\nif not HAS_MPL:\n    lr = lr_finder.suggestion()  # skip plotting\nelse:\n    lr_finder.plot()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Install matplotlib in any environment where you inspect LR curves","Use lr_finder.suggestion() and the results dict for programmatic workflows so plotting is optional"],"tags":["lr-finder","plot","matplotlib","missing-dependency","optional-install","pytorch-lightning"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}