I'm creating a scatter plot and i need to convert an integer number into decimal number, so what i need to do?
Here is my code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
day = pd.read_csv("day.csv")
fig = plt.figure(figsize = (10,8))
fig, ax = plt.subplots()
sns.scatterplot(x = "temp", y = "cnt", data = day, )
ax.scatter(x, y, marker = 'o')
ax.set_title("Bike Rentals at Different Temperatures" , fontsize = 14)
ax.set_xlabel("Count of Total Bike Rentals", fontsize = 12)
ax.set_ylabel("Normalized Temperature", fontsize = 12)
plt.show()
This is what i got :
CodePudding user response:
You can use type-casting :
n = 5
n = float(5)
Or in any programming language you can simply divide n by 1.0
to produce a floating point number:
n = 5
n = n / 1.0
CodePudding user response:
Variables in python are dynamically typed, which means the data type of a variable is determined at runtime. Because of this integers often will behave as floating point numbers when needed. Alternatively they can be converted using the float-function.
We can examine this behaviour using the isinstance method to determine the data type. Consider this example:
n = 2
m = 3
print(isinstance(n, int)) # True
a = n / m
print(isinstance(a, int)) # False
print(isinstance(a, float)) # True
b = n 1.0
print(isinstance(b, int)) # False
print(isinstance(b, float)) # True
Division in python produces a float in python, regardless of whether the inputs are integers, floats or a combination of the two. To use floor division, a separate operator is used i.e. n // m
, which always produces an integer.
Addition however will produce an integer if both number are integers, and output a float if either numbers is a float. In a similar fashion, conversion of integers to floats is handled by a lot of functions and libraries automatically.
Due to this behaviour, it should not be necessary to coerce the data type at most times. For an explicit conversion, you may use the built-in function float.
n = 2
print(isinstance(n, int)) # True
n = float(n)
print(isinstance(n, float)) # True