Categories
Python Answers

How to calculate the Euclidean distance with Python NumPy?

Sometimes, we want to calculate the Euclidean distance with Python NumPy.

In this article, we’ll look at how to calculate the Euclidean distance with Python NumPy.

How to calculate the Euclidean distance with Python NumPy?

To calculate the Euclidean distance with Python NumPy, we use the numpy.linalg.norm method.

For instance, we write

dist = numpy.linalg.norm(a - b)

to call numpy.linalg.norm with a and b where a and b are numpy arrays.

Conclusion

To calculate the Euclidean distance with Python NumPy, we use the numpy.linalg.norm method.

Categories
Python Answers

How to read JSON from a file with Python?

Sometimes, we want to read JSON from a file with Python.

In this article, we’ll look at how to read JSON from a file with Python.

How to read JSON from a file with Python?

To read JSON from a file with Python, we can open the file with open.

Then we call json.load to read the file.

For instance, we write

import json

with open('strings.json') as f:
    d = json.load(f)
    print(d)

to call open to read strings.json.

Then we call json.load with file f to read it into a JSON string.

Conclusion

To read JSON from a file with Python, we can open the file with open.

Then we call json.load to read the file.

Categories
Python Answers

How to add hovering annotations in Python matplotlib?

Sometimes, we want to add hovering annotations in Python matplotlib.

In this article, we’ll look at how to add hovering annotations in Python matplotlib.

How to add hovering annotations in Python matplotlib?

To add hovering annotations in Python matplotlib, we can call mpl_connect to add an event listener to watch for hovers.

For instance, we write

import matplotlib.pyplot as plt

fig = plt.figure()
plot = fig.add_subplot(111)

for i in range(4):
    plot.plot([i * 1, i * 2, i * 3, i * 4], gid=i)

def on_plot_hover(event):
    for curve in plot.get_lines():
        if curve.contains(event)[0]:
            print('over %s' % curve.get_gid())


fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)
plt.show()

to create a curve plot with

fig = plt.figure()
plot = fig.add_subplot(111)

for i in range(4):
    plot.plot([i * 1, i * 2, i * 3, i * 4], gid=i)

Then we define the on_plot_hover that gets the curve we hovered over by looping through them and then get the one that has the event mouse position.

Next, we use that as the hover event listener with

fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)

Conclusion

To add hovering annotations in Python matplotlib, we can call mpl_connect to add an event listener to watch for hovers.

Categories
Python Answers

How to create a cartesian product in Python Pandas?

Sometimes, we want to create a cartesian product in Python Pandas.

In this article, we’ll look at how to create a cartesian product in Python Pandas.

How to create a cartesian product in Python Pandas?

To create a cartesian product in Python Pandas, we can use the pd.MultiIndex.from_product method.

For instance, we write

a = [1, 2, 3]
b = ["a", "b", "c"]

index = pd.MultiIndex.from_product([a, b], names = ["a", "b"])

df = pd.DataFrame(index = index).reset_index()

to call pd.MultiIndex.from_product with lists a and b in a list.

And then we name the column names a and b.

Next, we create a data frame with the DataFrame class with the index returned from from_product.

And then we call reset_index to reset the indexes so that they’re set according to the rows in the new data frame.

Conclusion

To create a cartesian product in Python Pandas, we can use the pd.MultiIndex.from_product method.

Categories
Python Answers

How to find local IP addresses using Python?

Sometimes, we want to find local IP addresses using Python.

In this article, we’ll look at how to find local IP addresses using Python.

How to find local IP addresses using Python?

To find local IP addresses using Python, we can use the socket module.

For instance, we write

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
s.close()

to call docket.socket to create a socket object.

Then we connect to a DNS server with s.connect.

Next, we get the IP address from the getsockname method.

Finally, we call close to close the connection.

Conclusion

To find local IP addresses using Python, we can use the socket module.