How to know where the arrow ends in matplotlib quiver

2024/10/6 9:07:49

I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?

Answer

In general, the arrows are of length length as described in the Quiver documentation and are auto-calculated by the matplotlib. I don't know which kwarg may help to return the length.

Another approach could be to define the exact position by scaling the plot with the help of scale=1, units='xy'.

import numpy as np
import matplotlib.pyplot as plt# define arrow
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.3
v[5,5] = 0.3plt.quiver(x, y, u, v, scale=1, units='xy')plt.axis('equal')
plt.xlim(0,1)
plt.ylim(0,1)
plt.show()

enter image description here

Color arrows that end at a specific point

Applying the principles above could result in:

import numpy as np
import matplotlib.pyplot as plt
import randomn = 11
cx = 0.7 #x-position of specific end point
cy = 0.5 #y-position of specific end point# define random arrows
x = np.linspace(0,1,n)
y = np.linspace(0,1,n)
u = np.zeros((n,n))
v = np.zeros((n,n))# color everything black
colors = [(0, 0, 0)]*n*n# make sure at least some points end at the same point
u[5][5] = 0.2
u[5][8] = -0.1
v[2][7] = 0.3# search for specific point
for i in range(len(x)):for j in range(len(y)):endPosX = x[i] + u[j][i]endPosY = y[j] + v[j][i]if np.isclose(endPosX, cx) and np.isclose(endPosY, cy):#found specific point -> color it redcolors[j*n+i] = (1,0,0) # plot data
plt.quiver(x, y, u, v, color=colors, scale=1, units='xy')
plt.axis('equal')
plt.show()

enter image description here

https://en.xdnf.cn/q/119509.html

Related Q&A

how send text(sendkey) to ckeditor in selenium python scripting -chrome driver

I cant send a text to the text box of CKEditor while I scripting.it not shown in the seleniumIDE recording also.Help me to fix this issueASAP

How to replace the column of dataframe based on priority order?

I have a dataframe as follows df["Annotations"] missense_variant&splice_region_variant stop_gained&splice_region_variant splice_acceptor_variant&coding_sequence_variant&intron…

Scraping from web page and reformatting to a calender file

Im trying to scrape this site: http://stats.swehockey.se/ScheduleAndResults/Schedule/3940And Ive gotten as far (thanks to alecxe) as retrieving the date and teams.from scrapy.item import Item, Field fr…

Python Text to Data Frame with Specific Pattern

I am trying to convert a bunch of text files into a data frame using Pandas. Thanks to Stack Overflows amazing community, I almost got the desired output (OP: Python Text File to Data Frame with Specif…

Python Multiprocess OpenCV Webcam Get Request [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

error when trying to run my tensorflow code

This is a follow up question from my latest post: Put input in a tensorflow neural network I precoded a neural network using tensorflow with the MNIST dataset, and with the help of @FinnE was able to c…

ValueError: invalid literal for int() with base 10: Height (mm)

import csv from decimal import *def mean(data_set):return Decimal(sum(data_set)) / len(data_set)def variance(data_set):mean_res = mean(data_set)differences = []squared_res = []for elem in data_set:diff…

Having trouble in getting page source with beautifulsoup

I am trying to get the HTML source of a web page using beautifulsoup. import bs4 as bs import requests import urllib.request sourceUrl=https://www.pakwheels.com/forums/t/planing-a-trip-from-karachi-to-…

Cannot convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime

Im trying to convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime but the change is not saved: type(df_unix.loc[45,LastLoginDate])OUTPUT: pandas._libs.tslibs.timestamps.Timestampt…

string index out of range Python, Django

im using Django to develop a web application. When i try and run it on my web form i am receiving string index out of range error. However, when i hardcode a dictionary in to a python test file it work…