Building dotted two-dimensional vectors with matplotlib?

I use quiver to draw vectors in matplotlib:

 from itertools import chain import matplotlib.pyplot as pyplot pyplot.figure() pyplot.axis('equal') axis = pyplot.gca() axis.quiver(*zip(*map(lambda l: chain(*l), [ ((0, 0), (3, 1)), ((0, 0), (1, 0)), ])), angles='xy', scale_units='xy', scale=1) axis.set_xlim([-4, 4]) axis.set_ylim([-4, 4]) pyplot.draw() pyplot.show() 

which gives me beautiful arrows, but how can I change their line style to dashed, dashed, etc.?

+6
source share
1 answer

Oh! In fact, linestyle='dashed' works, just the quiver’s arrows are filled by default and do not have a line width. These are patches instead of paths.

If you do something like this:

 import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.axis('equal') ax.quiver((0,0), (0,0), (3,1), (1,0), angles='xy', scale_units='xy', scale=1, linestyle='dashed', facecolor='none', linewidth=1) ax.axis([-4, 4, -4, 4]) plt.show() 

enter image description here

You get dashed arrows, but probably not exactly what you had in mind.

You can play around with some options to get a little closer, but it still doesn't look very good:

 import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.axis('equal') ax.quiver((0,0), (0,0), (3,1), (1,0), angles='xy', scale_units='xy', scale=1, linestyle='dashed', facecolor='none', linewidth=2, width=0.0001, headwidth=300, headlength=500) ax.axis([-4, 4, -4, 4]) plt.show() 

enter image description here

Therefore, another way would be to use hatches:

 import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.axis('equal') ax.quiver((0,0), (0,0), (3,1), (1,0), angles='xy', scale_units='xy', scale=1, hatch='ooo', facecolor='none') ax.axis([-4, 4, -4, 4]) plt.show() 

enter image description here

+10
source

All Articles