Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug report
Bug summary
When saving a 3D scatter plot with a color bar usingsavefig
(eitherplt.savefig
orfig.savefig
), the data point colors are incorrect on the saved image, however the inline image (I'm using jupyter lab notebooks) is fine, the issue is only present when saving the figure. Note that this only happens when a colorbar is added to the figure, when a figure with points colored according to a set of values is saved, the issue does not occur. So it would seem that this issue is caused by some interaction of savefig and colorbar.
I've tried both matplotlib version 3.3.1 and 3.3.2 and in both versions this occurs. I've also tried downgrading to version 3.2.1, in version 3.2.1 this does not occur, so it seems some change in 3.3.1 is the cause of this issue.
Code for reproduction
Requirements: matplotlib 3.3.1 or 3.3.2
The following requirements are purely for setting up an example dataset which I used in the following code snippet to generate an example dataset to plot, as such I'm not listing the version numbers.
Optional: pandas, numpy
Also note that I used JupyterLab notebook to run the code, although given that I was able to 'fix' the issue by downgrading to matplotlib version 3.2.1 I don't think there is any link between running the code in JupyterLab and the issue itself.
#importing packagesimportmatplotlib.pyplotaspltimportnumpyasnpimportitertoolsimportpandasaspd# example data setupdata=list(itertools.product(*[np.arange(0,5,1),np.arange(0,5,1),np.arange(0,5,1)]))df=pd.DataFrame(data,columns=['x','y','z'])df['c']=df['x']+df['y']# plotting## no color bar example (no issue)fig=plt.figure()ax=fig.add_subplot(111,projection='3d',proj_type='ortho')sc=ax.scatter(df['x'],df['y'],df['z'],c=df['c'],s=40# size is purely to make it easier to see)plt.savefig('example no colorbar save.png')# dpi has no effect, purely to make it easier to zoom into the figure## color bar example (ISSUE)fig=plt.figure()ax=fig.add_subplot(111,projection='3d',proj_type='ortho')sc=ax.scatter(df['x'],df['y'],df['z'],c=df['c'],s=40# size is purely to make it easier to see)fig.colorbar(sc)plt.savefig('example colorbar save.png',dpi=300)# dpi has no effect, purely to make it easier to zoom into the figure
Actual outcome
This is being the saved figure with a color bar (note that if you zoom in the outlines of the data points are correct - see expected outcome below - but the fill of the points is incorrect).
Note that if no colorbar is added, the plot saves correctly as shown below.
Expected outcome
This is a screenshot of the colorbar chart as it is shown inline in the JupyterLab notebook.
And this is a screenshot of thenon colorbar chart as it is shown inline in the JupyterLab notebook, note this one is essentially the exact same as the saved version, so the issue is purely with the colorbar/savefig interaction.
Matplotlib version
- Operating system: Windows 10
- Matplotlib version: 3.3.2
- Matplotlib backend (
print(matplotlib.get_backend())
): - Python version: 3.8.5
- Jupyter version (if applicable): 2.2.6
- Other libraries: pandas, numpy, itertools (python built in library)
Everything has been installed via conda, with all packages listed being install from the default channel.