FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: set linestyle='dashed' raise error with quiver and legend · Issue #28298 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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

(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

Activity

  1. rcomer commented on May 26, 2024

    Member

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

  2. quzhijing commented on May 30, 2024

    Author

    @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()
  3. tacaswell commented on May 30, 2024

    Member

    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.

  4. added this to the v3.9.1 milestone on May 30, 2024
  5. quzhijing commented on May 31, 2024

    Author

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

  6. changed the title [-][Bug]: set linestyle='dashed' raise error[/-] [+][Bug]: set linestyle='dashed' raise error with quiver and legend[/+] on Jun 5, 2024
  7. QuLogic commented on Jun 28, 2024

    Member

    quiver produces a Quiver, which is a PolyCollection, so the legend handler for that copies the linestyle as:

    legend_handle.set_linestyle(get_first(orig_handle.get_linestyles()))

    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.

  8. modified the milestones: v3.9.1, v3.9.2 on Jul 4, 2024
  9. modified the milestones: v3.9.2, v3.9.3 on Aug 12, 2024
  10. 5 remaining items

  11. modified the milestones: v3.10.2, v3.10.3 on May 2, 2025
  12. modified the milestones: v3.10.4, v3.10.5 on Jul 25, 2025
  13. modified the milestones: v3.10.6, v3.10.7 on Aug 29, 2025
  14. QuLogic commented on Sep 10, 2025

  15. modified the milestones: v3.10.7, v3.10.8 on Oct 8, 2025
  16. modified the milestones: v3.10.8, v3.10.9 on Nov 13, 2025
  17. QuLogic commented on Mar 20, 2026

    Member

    Fixed by #31307.

  18. modified the milestones: v3.10.9, v3.11.0 on Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL