-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: set linestyle='dashed' raise error with quiver and legend #28298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello @quzhijing thanks for the report. Please could you provide a complete code example that we can run to reproduce the problem? |
@rcomer my complete code: import matplotlib.pyplot as plt
import numpy as np
# Set the plotting range
plt.figure(figsize=(10, 10))
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.grid(color='gray', linestyle='--', linewidth=0.5)
# Define vectors
v1 = np.array([3, 4])
v2 = np.array([4, -3])
# Plot vectors v1 and v2
plt.quiver(0, 0, v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color='r', label='v1 (3, 4)')
plt.quiver(0, 0, v2[0], v2[1], angles='xy', scale_units='xy', scale=1, color='b', label='v2 (4, -3)')
# Plot the sum of the vectors
v_sum = v1 + v2
line1 = plt.quiver(0, 0, v_sum[0], v_sum[1], angles='xy', scale_units='xy', scale=1, color='g', label='v1 + v2')
line1.set_linestyle('dashed')
# Plot the difference of the vectors
v_diff = v1 - v2
plt.quiver(0, 0, v_diff[0], v_diff[1], angles='xy', scale_units='xy', scale=1, color='y', linestyle='dashed', facecolor='none', linewidth=1, label='v1 - v2')
# Plot scalar multiplication
scalar = 2
v_scalar = scalar * v1
plt.quiver(0, 0, v_scalar[0], v_scalar[1], angles='xy', scale_units='xy', scale=1, color='m', linestyle='dashed', facecolor='none', linewidth=1, label='2 * v1')
# Plot vector dot product (projection explanation)
v2_unit = v2 / np.linalg.norm(v2)
projection_length = np.dot(v1, v2_unit)
projection = projection_length * v2_unit
plt.quiver(0, 0, projection[0], projection[1], angles='xy', scale_units='xy', scale=1, color='c', linestyle='dashed', facecolor='none', linewidth=1, label='proj_v2(v1)')
# Add legend
plt.legend()
# Display the plot
plt.title('Basic Vector Operations in Linear Algebra')
plt.show() |
A minimal reproducer: plt.figure(figsize=(10, 10))
line1 = plt.quiver(0, 0, 7, 1, angles='xy', scale_units='xy', scale=1, color='g', label='v1 + v2', lw=0)
line1.set_linestyle('dashed')
plt.legend()
fig = plt.gcf()
plt.show() setting |
@tacaswell Thanks, it's fixed after remote legend() show, So would we can expect to fix in newer version? |
matplotlib/lib/matplotlib/legend_handler.py Line 801 in 74c7f9a
The semantics between the setter and getter are a bit confused, so this is somewhat related to #23056, though unfortunately, that PR does not fix this bug. |
If the linewidth is positive and not 1, we see that the pattern gets scaled again in the legend. plt.figure(figsize=(10, 10))
line1 = plt.quiver(0, 0, 7, 1, angles='xy', scale_units='xy', scale=1, facecolor='g', label='v1 + v2', lw=2, edgecolor='k')
line1.set_linestyle('dotted')
plt.legend()
fig = plt.gcf()
plt.show() It seems we need a way to get hold of the unscaled pattern. |
Bug summary
Issue description:
I am trying to print the linear arrow with dashed style, but there is error waring
"ValueError: At least one value in the dash list must be positive"
Code:
Part Code like following
plt.quiver(0, 0, v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color='r', label='v1 (3, 4)', linestyle='solid')
plt.quiver(0, 0, v2[0], v2[1], angles='xy', scale_units='xy', scale=1, color='b', label='v2 (4, -3)', linestyle='solid')
v_sum = v1 + v2
plt.quiver(0, 0, v_sum[0], v_sum[1], angles='xy', scale_units='xy', scale=1, color='g', linestyle='dashed', linewidth=0, label='v1 + v2')
#I searched similar issue ticket: #8821 to set lw=0, it's still not work
enviroment:
my python version: Python 3.12.3
matplotlib version: 3.9.0
Code for reproduction
Actual outcome
There is empty present

(mathenv) C:\WorkSpace\math>c:/WorkSpace/math/mathenv/Scripts/python.exe c:/WorkSpace/math/linearAnalog.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python312\Lib\tkinter_init_.py", line 1967, in call
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\tkinter_init_.py", line 861, in callit
func(*args)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\backends_backend_tk.py", line 271, in idle_draw
self.draw()
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 10, in draw
super().draw()
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\backends\backend_agg.py", line 387, in draw
self.figure.draw(self.renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 95, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\figure.py", line 3155, in draw
mimage._draw_list_compositing_images(
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\axes_base.py", line 3109, in draw
mimage._draw_list_compositing_images(
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\legend.py", line 777, in draw
self._legend_box.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 39, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\offsetbox.py", line 383, in draw
c.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 39, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\offsetbox.py", line 383, in draw
c.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 39, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\offsetbox.py", line 383, in draw
c.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 39, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\offsetbox.py", line 383, in draw
c.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 39, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\offsetbox.py", line 669, in draw
c.draw(renderer)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\patches.py", line 632, in draw
self._draw_paths_with_artist_properties(
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\patches.py", line 594, in _draw_paths_with_artist_properties
gc.set_dashes(*self._dash_pattern)
File "c:\WorkSpace\math\mathenv\Lib\site-packages\matplotlib\backend_bases.py", line 924, in set_dashes
raise ValueError(
ValueError: At least one value in the dash list must be positive
Expected outcome
Expect the plt.quiver can print a dash arrow for the sum of two linear array
Additional information
No response
Operating system
No response
Matplotlib Version
3.9.0
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
None
The text was updated successfully, but these errors were encountered: