Issue
MPL throws a weird error when I want to export a scatter plot as PDF format. When creating the MWE, I could reduce the error to one line.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 1000)
y = np.sin(x)
plt.plot( # works just fine
x,
y,
ls="",
marker="x",
)
plt.savefig("test_fig_plot", format='pdf')
plt.close()
plt.scatter(
x,
y,
ls="", # <== PROBLEM
marker="x",
)
plt.savefig("test_fig_scatter", format='pdf')
This is the error:
Traceback (most recent call last):
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-7465ebbcb267>", line 1, in <module>
runfile('/mnt/c/Users/erit/AppData/Roaming/JetBrains/PyCharm2023.3/scratches/scratch_2.py', wdir='/mnt/c/Users/erit/AppData/Roaming/JetBrains/PyCharm2023.3/scratches')
File "/home/user/.pycharm_helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/mnt/c/Users/erit/AppData/Roaming/JetBrains/PyCharm2023.3/scratches/scratch_2.py", line 24, in <module>
plt.savefig("test_fig_scatter", format='pdf')
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/pyplot.py", line 1119, in savefig
res = fig.savefig(*args, **kwargs) # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/figure.py", line 3390, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2187, in print_figure
result = print_method(
^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2043, in <lambda>
print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/backends/backend_pdf.py", line 2807, in print_pdf
self.figure.draw(renderer)
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/figure.py", line 3154, in draw
mimage._draw_list_compositing_images(
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3070, in draw
mimage._draw_list_compositing_images(
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/collections.py", line 1005, in draw
super().draw(renderer)
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/collections.py", line 423, in draw
renderer.draw_path_collection(
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/matplotlib/backends/backend_pdf.py", line 2074, in draw_path_collection
padding = np.max(linewidths)
^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 2810, in max
return _wrapreduction(a, np.maximum, 'max', axis, None, out,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/miniconda3/envs/gis2/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 88, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zero-size array to reduction operation maximum which has no identity
I am using:
python 3.11.7
matplotlib 3.8.0
- on
WSL Debian 12
I need scatter plots to color my data but I don't want to have a line, just markers. Markers with line is working as plt.plot
with the desired settings (but no color map) is. PNG export is also fine.
I really cannot uderstand the error...
Thanks already for helping!
Solution
plt.scatter
does not have a ls
kwarg
since there is no line (that's what it means for it to be a scatter plot). Remove that argument and the code works fine.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(x)
plt.figure()
plt.scatter(x, y, marker="x")
plt.savefig("test_fig_scatter.pdf", format="pdf")
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.