Find all points below a line on a map
In order to draw a path between two points on a map with many points (almost two thousand), I use the following function:
def path_between_cities(self, cities_with_coordinates, from_to): from matplotlib.lines import Line2D # coordinates from chosen path x = [int(from_to), int(from_to)] y = [int(from_to), int(from_to)] #create line line = Line2D(x,y,linestyle='-',color='k') # create axis x_ = np.array((0,2000)) y_ = np.array((0,6000)) plt.plot(x_,y_, 'o') for item in cities_with_coordinates: name = item y_coord = int(item) x_coord = int(item) plt.plot([x_coord], [y_coord], marker='o', markersize=1, color='blue') plt.axes().add_line(line) plt.axis('scaled') plt.show()
My goal is to extract all points (coordinates) which are found below the drawn line.
I know that you can do this using the cross product of vectors
Given a large number of vectors, what would be the most efficient way of achieving this in the context above?
Each cross product operation is still
O(1). You can run the below function for all the points and see which of them are below, bringing it to a linear time check.
def ccw(a,b,c): """ Returns 1 if c is above directed line ab else returns -1""" return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y) #a and b are the vertices and c is the test point.
Unless you have some other information about the points, you would have to check each point to see if it below a particular line.
See also questions close to this topic
Python Kafka Streaming API - Binning
I am using python kafka stream binning example given in this, Python Kafka Streaming API
I am able to generate the data using generator.py file given under winton-kafka-streams/examples/binning/, whereas when i run the binning.py file from the same folder, i got the below issue. Could someone help me, to resolve this?
Change color of missing values in Seaborn heatmap
Consider the example of missing values in the Seaborn documentation:
corr = np.corrcoef(np.random.randn(10, 200)) mask = np.zeros_like(corr) mask[np.triu_indices_from(mask)] = True sns.heatmap(corr, mask=mask, vmax=.3, square=True)
How do I change the color of the missing values to, for example, black? The color of the missing values should be specified independent of the color scheme of the heatmap, it may not be present in the color scheme.
I tried adding
facecolor = 'black'but that didn't work. The color can be affected by e.g.
sns.axes_style("white")but it isn't clear to me how that can be used to set an arbitrary color.
Xpath + Scrapy + Python : data point couldn't be scraped
This is the XML structure:
<tr> <td> <font size="3"> <strong>Location:</strong> Hiranandani Gardens, Powai </font> </td> </tr>
I want to extract : Hiranandani Gardens, Powai
I tried with these:
Both returned an empty list.
Note: we must have to use the text of tag, i.e., "Location:". Otherwise, there are many other places on the site where the same XML structure is used. So, it'll fetch many more unnecessary things apart from the desired value if the text of strong tag is not used.
Zooming and plotting a inset plot
I am trying to do a zoomed in inset plot like the image:
The first part of the code is working i.e. it is plotting the file. Only when I try to plot the zoomed part it gives the following error I have trying to figure it out but none of the posts have been really helpful.
AttributeError Traceback (most recent call last) <ipython-input-41-46879fbc5ce6> in <module>() 44 plt.close() 45 ---> 46 fit_data() <ipython-input-41-46879fbc5ce6> in fit_data() 16 #axins.xaxis.set_major_locator(MaxNLocator(nbins=1, prune='lower')) 17 ---> 18 plt1 = zoomed_inset_axes(plt, 2.5, loc=4 ) 19 plt1.plot(data1['pm'], data1['Dis(pc)'])#,marker='o', color='red', edgecolor='red', s=100) 20 plt1.axis([5.062645643, 6.482765605, 487.026819, 569.4313421]) ~/anaconda3/lib/python3.6/site-packages/mpl_toolkits/axes_grid1/inset_locator.py in zoomed_inset_axes(parent_axes, zoom, loc, bbox_to_anchor, bbox_transform, axes_class, axes_kwargs, borderpad) 529 530 if axes_kwargs is None: --> 531 inset_axes = axes_class(parent_axes.figure, parent_axes.get_position()) 532 else: 533 inset_axes = axes_class(parent_axes.figure, parent_axes.get_position(), AttributeError: module 'matplotlib.pyplot' has no attribute 'get_position'
This is the code I have been using.
import numpy as np import matplotlib as mpl import pandas as pd from scipy.optimize import curve_fit import matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator from matplotlib.colors import ListedColormap, LinearSegmentedColormap from matplotlib.ticker import MaxNLocator from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset file1 = 'inset_trial.dat' data1 = pd.read_csv(file1, delimiter='\s+', header=None, engine='python') data1.columns = ['x1', 'y1', 'xin', 'yin'] def fit_data(): fig = plt.figure(1,figsize=(12,12)) plt.subplot(111) mpl.rcParams['figure.dpi']=200 plt.scatter(data1['x1'], data1['y1'], marker='o', color='red', edgecolor='red', s=100) plt1 = zoomed_inset_axes(plt, 2.5, loc=4 ) plt1.plot(data1['xin'], data1['yin'])#,marker='o', color='blue', edgecolor='blue', s=100) plt1.axis([5.062645643, 6.482765605, 487.026819, 569.4313421]) plt1.set_yticks() plt1.set_xticks() plt1.set_axis_bgcolor('none') axes = mark_inset(axins, axins_2, loc1=2, loc2=4, fc="none", ec="0.5") plt.minorticks_on() plt.tick_params(axis='both',which='minor',length=5,width=2,labelsize=28) plt.tick_params(axis='both',which='major',length=8,width=2,labelsize=28) plt.tick_params(direction='out', length=8, width=3) plt.tick_params(labelsize=28) plt.show() plt.close() fit_data()
The sample data I have been trying to fit is
797.3266855 9.518953577 487.026819 6.41595323 457.3328822 9.408619701 493.8012816 6.352140859 321.4279994 10.99152002 505.8109589 6.482765605 643.1595144 11.33567151 515.0500793 5.689992589 897.9396964 7.098272377 523.5118663 5.062645643 658.5927932 8.401072532 526.8570713 5.951114622 885.8478465 9.59502937 537.6740407 6.123622699 569.4313421 5.913067314 563.2567733 6.089519297 419.540411 31.7279367 569.4313421 5.913067314 386.0084504 13.82448229 487.026819 6.41595323 790.5056852 14.17210085 736.5781168 4.142827023 927.9643155 13.42713535 106.249016 49.12866299 678.4950877 3.174864242 108.0434865 60.24915209 809.8782024 8.371119015 692.3002948 7.215181213 915.4764187 15.4360679 874.5699615 8.706973258 962.0774108 3.223371528 401.4037586 31.03032051 671.4700933 11.1975808 834.7473745 15.30785654
how to plot multiple barcharts in python
I have a data which has retail industry goods purchases transactions. every record in the data is a separate transaction. The transactions are from 2015 to 2017. I am plotting the number of transactions by each month for 2015, 2016 and 2017 in three separate graphs. How can I combine these three graphs and show all the barcharts in a single one ? this is my code the results for this is as shown in the figure three individual barcharts
Given a two-dimensional graph with points, find a line that passes through the largest number of points
This problem comes from cracking the coding interview chapter 7 problem 6. To me as a mathematician this seems like a simple least squares problem where we find the best fit line. Although, in the solution they take a different approach.
My question is the following: is a developing a least squares approach a sufficient solution or am I not understanding the problem at hand?
Find regular grid cells intersecting arbitrary shaped quadrilateral
I need to find all indexes
[x, y]for grid cells intersecting an arbitray shaped quadrilateral, defined by its corner coordinates;
- grid cells are tiles with
128 x 128 px
grid cells have an integer index between
(max extend is a square with
(2nx * 128) * (2ny * 128) px)
quadrilateral is defined by corner points, with coordinates
(qx, qy)in pixel space given as
(tl, tr, br, bl)
- This is integrated in a three.js scene:
- corner points/coordinates are raycasted from camera on a base THREE.PlaneBufferGeometry
For now I compute the perimeter tiles by traversing each quadrilateral edge using
mx + bwith the tile dimension (
128px) as step; from there, I just add interior tile indexes row for row. But that´s somewhat clumsy, which might or might not be a coding skill issue. I am about to try using the THREE.Raycaster to get the perimeter tile indexes, but not sure how, yet.
I am seeking a better solution algorithm-wise; basic formulae, pseudo code, ideas, or definite solutions.
- grid cells are tiles with
Programatically define second and third points of a triangle from two points and angles
If I have two points, one being the starting point or vertex (A) and the other being the median point of A, and I also have the three angles, how do I programatically determine points B and C?
Counting pairs column elements with the same value in a data frame and shows in a matrix format
I was searching on internet for similar solution, but I was not able to find the specific one for my case. Let's say a have the following data frame:
a = c(1, 1, 1, 2, 2) b = c(2, 1, 1, 1, 2) c = c(2, 2, 1, 1, 1) d = c(1, 2, 2, 1, 1) df <- data.frame(a = a, b = b, c = c, d = d)
dflooks like this:
a b c d 1 1 2 2 1 2 1 1 2 2 3 1 1 1 2 4 2 1 1 1 5 2 2 1 1
Note: In this example I use
[1,2]pair of values, but it could be a set of different values:
[-1,1]or even more than two possible values:
Now I would like to have a matrix where each
[i,j]element will represent the number of rows with the value
j. For this particular case we have (showing the upper diagonal, because its symmetric):
a b c d a 3 2 1 1 b 3 2 1 c 3 2 d 3
The diagonal should count the number of rows with
1value at a given column. On this case all columns have the sames number of value
1. The format should be similar to
cor()function (Correlation Matrix).
I was trying to use
descrpackage) but it shows the information by pairs of columns.
It can be done by computing manually the occurrence of
1of each pair of columns (i.e.:
nrow(df[df$a==1 & df$b==1,])=2) and then putting into a matrix, but I was wondering if there is a built-in function that simplify the process.
Binary Space Partition 3D Simple Mathematics
Let say we have 3 polygons with the following points:
A(40:100:100) B(20:0:100) C(60:5:100) polygon1 (black)
D(60:50:50) E(10:50:150) F(60:50:150) polygon2 (red)
G(40:20:0) H(30:10:0) I(50:10:0) polygon3 (green)
According to my understanding about BSP algorithm, the polygon started by polygon1 as the most top of the BSP tree. Then make a partition of polygon2 let say polygon 2A and 2B. And now polygon1 has polygon 2A as the left node and polygon 2B as the right node. Finally, polygon 2A has polygon3 as the left node. Therefore, the order of displaying the surfaces from back to front: 3-2A-1-2B. It is easy to tell just by looking the picture and knowing from the point coordinate of the surface.
My question is, when the case of partitioning polygon begin mathematically? And how can we decide whether that polygon is in the left node or right node mathematically? My lecture said the following function is the answer.
normal = calculateVertexNormal(A, B, C) //cross product calculation Dim temp1, temp2, temp3 As TPoint Dim temp As Double = 0 temp1 = calculateVectorSubstraction(A, viewPoint) : temp2 = calculateVectorSubstraction(B, viewPoint) : temp3 = calculateVectorSubstraction(C, viewPoint) //AV as vector calculation(V-A) a = calculateDotProductVector(temp1, normal) //return double b = calculateDotProductVector(temp2, normal) c = calculateDotProductVector(temp3, normal)
And the following function will determine which that polygon cases belong to:
If a < 0 And b < 0 And c < 0 Then insertPoly(LeftNode,Poly) ElseIf a > 0 And b > 0 And c > 0 Then insertPoly(RightNode, Poly) ElseIf a < 0 And b > 0 And c > 0 Then I1 = findIntersection(A,B,normal,viewPoint) I2 = findIntersection(A,C,normal,viewPoint) Poly1 = newPoly(B, C, I1) Poly2 = newPoly(C, I2, I1) Poly3 = newPoly(A, I1, I2) insertPoly(Left, Poly3) insertPoly(Right, Poly1) insertPoly(Right, Poly2) ElseIf a < 0 And b < 0 And c > 0 Then I1 = findIntersection(C,A,normal,viewPoint) I2 = findIntersection(C,B,normal,viewPoint) Poly1 = newPoly(A, B, I1) Poly2 = newPoly(C, I2, I1) Poly3 = newPoly(A, I1, I2) insertPoly(Left, Poly3) insertPoly(Left, Poly1) insertPoly(Right, Poly2) ElseIf a < 0 And isZero(b) And c > 0 Then // I don't know yet about this End If
But when I implemented the function above to the program it doesn't work. And I assumed that there must be something wrong with the algorithm above. Which part of the function above that I have to fix? Or is there any other solution?