Skip to content

Random error of PyCapsule_New called with null pointer error when using seaborn matplotlib numpy and pandas #121290

Closed as not planned

Description

@fx-Chintu

Bug report

Bug description:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes


data = load_diabetes()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target

df = df.drop_duplicates()
df = df.ffill()

df.hist(bins=30, grid=False)
plt.suptitle('Histograms of Numerical Features')
plt.show()
sns.pairplot(df[['target', 'age', 'bmi', 's4', 's5']])
plt.suptitle('Pair Plot of Target and Selected Features', y=1.02)
plt.show()
corr_matrix = df.corr()
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Heatmap of Correlation Matrix')
plt.show()
sns.boxplot(data=df.select_dtypes(include=[np.number]))
plt.title('Boxplots for Numerical Features')
plt.xticks(rotation=90)
plt.show()
plt.scatter(df['bmi'], df['age'], c=df['target'], cmap='viridis', alpha=0.6)
plt.colorbar(label='Diabetes Progression')
plt.xlabel('BMI')
plt.ylabel('Age')
plt.title('Scatter Plot of BMI vs Age Color-Coded by Target')
plt.show()

#################
The above code is randomly giving the following error:

Traceback (most recent call last):
  File "D:/python work/p10.py", line 20, in <module>
    sns.pairplot(df[['target', 'age', 'bmi', 's4', 's5']])
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\seaborn\axisgrid.py", line 2119, in pairplot
    grid = PairGrid(data, vars=vars, x_vars=x_vars, y_vars=y_vars, hue=hue,
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\seaborn\axisgrid.py", line 1280, in __init__
    fig = plt.figure(figsize=figsize)
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 1022, in figure
    manager = new_figure_manager(
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\pyplot.py", line 545, in new_figure_manager
    return _get_backend_mod().new_figure_manager(*args, **kwargs)
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3521, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 3526, in new_figure_manager_given_figure
    return cls.FigureCanvas.new_manager(figure, num)
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\backend_bases.py", line 1811, in new_manager
    return cls.manager_class.create_with_canvas(cls, figure, num)
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 479, in create_with_canvas
    with _restore_foreground_window_at_end():
  File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
  File "C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\matplotlib\backends\_backend_tk.py", line 43, in _restore_foreground_window_at_end
    foreground = _c_internal_utils.Win32_GetForegroundWindow()
ValueError: PyCapsule_New called with null pointer

Sometime it executes successfully and sometimes it doesn't. If there is no bug then please provide me solution for this.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Activity

  1. Eclips4 commented on Jul 2, 2024

    @Eclips4
    Member

    Hello!
    This problem seems to be related to matplotlib and it looks like it has already been fixed: matplotlib/matplotlib#28267 & matplotlib/matplotlib#28269

    So, there's nothing to do on the CPython side.

  2. removed
    type-bugAn unexpected behavior, bug, or error
    on Jul 2, 2024
  3. fx-Chintu commented on Jul 2, 2024

    @fx-Chintu
    Author

    Hello! This problem seems to be related to matplotlib and it looks like it has already been fixed: matplotlib/matplotlib#28267 & matplotlib/matplotlib#28269

    So, there's nothing to do on the CPython side.

    Can you please help me with this? or tell me exactly where or how to fix..?

  4. Eclips4 commented on Jul 2, 2024

    @Eclips4
    Member

    Hello! This problem seems to be related to matplotlib and it looks like it has already been fixed: matplotlib/matplotlib#28267 & matplotlib/matplotlib#28269
    So, there's nothing to do on the CPython side.

    But I'm still getting those random error. If it is fixed can you please tell me exactly where or how to fix..?

    You can wait for the next release of matplotlib where's the fix will be applied or you can install the latest version from its github repository.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions