Categories
Python Answers

How to truncate float values with Python?

Sometimes, we want to truncate float values with Python.

In this article, we’ll look at how to truncate float values with Python.

How to truncate float values with Python?

To truncate float values with Python, we can use the round function.

For instance, we write

n = round(1.923328437452, 3)

to call round to round 1.923328437452 to 3 decimal places.

Conclusion

To truncate float values with Python, we can use the round function.

Categories
Python Answers

How to rank items in an array using Python NumPy, without sorting array twice?

Sometimes, we want to rank items in an array using Python NumPy, without sorting array twice.

In this article, we’ll look at how to rank items in an array using Python NumPy, without sorting array twice.

How to rank items in an array using Python NumPy, without sorting array twice?

To rank items in an array using Python NumPy, without sorting array twice, we can use the SciPy rankdata function.

For instance, we write

from scipy.stats import rankdata

a = [4, 2, 7, 1]
ranks = rankdata(a)

to call rankdata with list a to return a list with the ranks of the values in a.

The ranks for floats by default.

Conclusion

To rank items in an array using Python NumPy, without sorting array twice, we can use the SciPy rankdata function.

Categories
Python Answers

How to read in table without headers with Python Pandas?

Sometimes, we want to read in table without headers with Python Pandas.

In this article, we’ll look at how to read in table without headers with Python Pandas.

How to read in table without headers with Python Pandas?

To read in table without headers with Python Pandas, we call read_csv with the header argument set to None.

For instance, we write

df = pd.read_csv(file_path, header=None, usecols=[3, 6])

to call read_csv with the file_path to the csv file.

header is set to None to skip the headers.

And we set usecols to [3, 6] to get the 4th and 7th columns.

Conclusion

To read in table without headers with Python Pandas, we call read_csv with the header argument set to None.

Categories
Python Answers

How to find all possible permutations of a given string in Python?

Sometimes, we want to find all possible permutations of a given string in Python.

In this article, we’ll look at how to find all possible permutations of a given string in Python.

How to find all possible permutations of a given string in Python?

To find all possible permutations of a given string in Python, we can use the itertools‘s permutation function.

For instance, we write

from itertools import permutations
perms = [''.join(p) for p in permutations('foobar')]

to call permutation with 'foobar' to find all permutations of the characters in the 'foobar' string.

We call ''.join with the permuted characters to join them back into strings.

Conclusion

To find all possible permutations of a given string in Python, we can use the itertools‘s permutation function.

Categories
Python Answers

How to fix Python Matplotlib overlapping annotations or text?

Sometimes, we want to fix Python Matplotlib overlapping annotations or text.

In this article, we’ll look at how to fix Python Matplotlib overlapping annotations or text.

How to fix Python Matplotlib overlapping annotations or text?

To fix Python Matplotlib overlapping annotations or text, we can use the adjustText library.

To install it, we run

pip install adjustText

Then we use it by writing

import matplotlib.pyplot as plt
from adjustText import adjust_text
import numpy as np

together = [(0, 1.0, 0.4), (25, 1.013, 0.41), (50, 1.016, 0.41), (75, 1.10434, 0.42), (100, 1.161044, 0.44), (125, 1.16856, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.401399, 0.45)]

together.sort()

text = [x for (x,y,z) in together]
eucs = [y for (x,y,z) in together]
covers = [z for (x,y,z) in together]

p1 = plt.plot(eucs,covers,color="black", alpha=0.5)
texts = []
for x, y, s in zip(eucs, covers, text):
    texts.append(plt.text(x, y, s))

plt.xlabel("x")
plt.ylabel("y")
plt.title("Test plot")
adjust_text(texts, only_move={'points':'y', 'texts':'y'}, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
plt.show()

to create the together list with the points we wan tto plot.

And then we create lists with the x, y and z axis values from the together list.

Next, we call plot to plot the values.

And then we append the text with the for loop.

And then we call adjust_text to move the texts so that they won’t over.

We specify that we move the points and texts in the y direction.

Conclusion

To fix Python Matplotlib overlapping annotations or text, we can use the adjustText library.