Skip to content
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

[Bug]: set linestyle='dashed' raise error with quiver and legend #28298

Open
quzhijing opened this issue May 25, 2024 · 4 comments
Open

[Bug]: set linestyle='dashed' raise error with quiver and legend #28298

quzhijing opened this issue May 25, 2024 · 4 comments

Comments

@quzhijing
Copy link

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

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')

Actual outcome

There is empty present
image

(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

@rcomer
Copy link
Member

rcomer commented May 26, 2024

Hello @quzhijing thanks for the report. Please could you provide a complete code example that we can run to reproduce the problem?

@rcomer rcomer added status: needs clarification Issues that need more information to resolve. topic: plotting methods labels May 26, 2024
@quzhijing
Copy link
Author

quzhijing commented May 30, 2024

@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()

@tacaswell
Copy link
Member

tacaswell commented May 30, 2024

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 lw to be >0 or not showing the legend fixes the problem. Something is going wrong in generating the legend entry, I think because it is grabbing a post-scaled version of the dash pattern.

@tacaswell tacaswell added this to the v3.9.1 milestone May 30, 2024
@tacaswell tacaswell added status: confirmed bug and removed status: needs clarification Issues that need more information to resolve. labels May 30, 2024
@quzhijing
Copy link
Author

quzhijing commented May 31, 2024

@tacaswell Thanks, it's fixed after remote legend() show, So would we can expect to fix in newer version?

@tacaswell tacaswell changed the title [Bug]: set linestyle='dashed' raise error [Bug]: set linestyle='dashed' raise error with quiver and legend Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants