I have created a DataFrame from an object called car_data
:
import pandas as pd
class Car:
def __init__(self, make, year, price, mileage, color, buy_rate):
self.make = make
self.year = year
self.price = price
self.mileage = mileage
self.color = color
self.buy_rate = buy_rate
cars_data = [
{"make": "Toyota", "year": 2018, "price": 20000, "mileage": 50000, "color": "Blue", "buy_rate": 0.8},
{"make": "Honda", "year": 2019, "price": 25000, "mileage": 40000, "color": "Red", "buy_rate": 0.7},
{"make": "Ford", "year": 2020, "price": 28000, "mileage": 30000, "color": "Black", "buy_rate": 0.6},
{"make": "Chevrolet", "year": 2017, "price": 18000, "mileage": 60000, "color": "White", "buy_rate": 0.75},
{"make": "Nissan", "year": 2019, "price": 23000, "mileage": 35000, "color": "Silver", "buy_rate": 0.65},
{"make": "BMW", "year": 2021, "price": 35000, "mileage": 20000, "color": "Gray", "buy_rate": 0.55},
{"make": "Mercedes", "year": 2018, "price": 30000, "mileage": 45000, "color": "Black", "buy_rate": 0.8},
{"make": "Audi", "year": 2020, "price": 32000, "mileage": 25000, "color": "White", "buy_rate": 0.7},
{"make": "Subaru", "year": 2019, "price": 22000, "mileage": 35000, "color": "Blue", "buy_rate": 0.75},
{"make": "Hyundai", "year": 2020, "price": 26000, "mileage": 30000, "color": "Red", "buy_rate": 0.65},
{"make": "Kia", "year": 2017, "price": 20000, "mileage": 55000, "color": "Green", "buy_rate": 0.6},
{"make": "Volkswagen", "year": 2018, "price": 24000, "mileage": 40000, "color": "Black", "buy_rate": 0.8},
{"make": "Tesla", "year": 2022, "price": 60000, "mileage": 15000, "color": "Blue", "buy_rate": 0.85},
{"make": "Lexus", "year": 2019, "price": 35000, "mileage": 25000, "color": "Silver", "buy_rate": 0.75},
{"make": "Mazda", "year": 2018, "price": 21000, "mileage": 45000, "color": "Red", "buy_rate": 0.7},
{"make": "Jeep", "year": 2020, "price": 29000, "mileage": 20000, "color": "White", "buy_rate": 0.65},
{"make": "Volvo", "year": 2021, "price": 38000, "mileage": 30000, "color": "Gray", "buy_rate": 0.6},
{"make": "Chrysler", "year": 2019, "price": 27000, "mileage": 35000, "color": "Black", "buy_rate": 0.8},
{"make": "Buick", "year": 2017, "price": 22000, "mileage": 40000, "color": "Blue", "buy_rate": 0.7},
{"make": "Ferrari", "year": 2022, "price": 150000, "mileage": 10000, "color": "Red", "buy_rate": 0.9},
{"make": "Acura", "year": 2020, "price": 33000, "mileage": 22000, "color": "White", "buy_rate": 0.75},
{"make": "Porsche", "year": 2021, "price": 45000, "mileage": 18000, "color": "Black", "buy_rate": 0.85},
{"make": "Infiniti", "year": 2018, "price": 32000, "mileage": 28000, "color": "Gray", "buy_rate": 0.7},
{"make": "Land Rover", "year": 2019, "price": 55000, "mileage": 25000, "color": "Green", "buy_rate": 0.65},
{"make": "Jaguar", "year": 2020, "price": 60000, "mileage": 20000, "color": "Blue", "buy_rate": 0.6},
{"make": "Maserati", "year": 2021, "price": 70000, "mileage": 15000, "color": "Red", "buy_rate": 0.8},
{"make": "Bentley", "year": 2019, "price": 80000, "mileage": 12000, "color": "White", "buy_rate": 0.75},
{"make": "Rolls Royce", "year": 2020, "price": 100000, "mileage": 10000, "color": "Silver", "buy_rate": 0.9},
{"make": "Lincoln", "year": 2018, "price": 45000, "mileage": 20000, "color": "Black", "buy_rate": 0.8},
{"make": "Cadillac", "year": 2017, "price": 40000, "mileage": 30000, "color": "Blue", "buy_rate": 0.75},
{"make": "Aston Martin", "year": 2021, "price": 150000, "mileage": 8000, "color": "Red", "buy_rate": 0.85},
{"make": "Alfa Romeo", "year": 2019, "price": 60000, "mileage": 20000, "color": "White", "buy_rate": 0.7},
{"make": "Bugatti", "year": 2020, "price": 3000000, "mileage": 500, "color": "Blue", "buy_rate": 0.95},
]
cars = []
for car_data in cars_data:
car = Car(car_data["make"], car_data["year"], car_data["price"], car_data["mileage"], car_data["color"], car_data["buy_rate"])
cars.append(car)
car_data_dict = {
"Make": [car.make for car in cars],
"Year": [car.year for car in cars],
"Price": [car.price for car in cars],
"Mileage": [car.mileage for car in cars],
"Color": [car.color for car in cars],
"Buy Rate": [car.buy_rate for car in cars]
}
car_df = pd.DataFrame(car_data_dict)
print(car_df)
After this I tried to plot it with plt.subplots
:
fig, ax = plt.subplots(figsize=(9,5))
scatter = ax.scatter(x=car_df['Price'],
y=car_df['Year'],
c=car_df['Year'])
ax.set(title="Car data >=2024 ",
xlabel='Price',
ylabel='Year')
And this is what I got:
This is weird behavior, for, as you can see in the car_data
dictionary above, it contains prices in thousands. The plot shows the prices as if it is a decimal!
On the contrary, if I print this:
car_df['Price'][0]
It returns 20000
as expected.
Note: This didn't happen before I added Mileage.
No, the price in the scatter plot are number 0-3 MILLIONS. Look closely the axis.
If you want to avoid that you can add
ax.get_xaxis().get_major_formatter().set_scientific(False)
Or you can set your own formatter
from matplotlib.ticker import FormatStrFormatter
ax.xaxis.set_major_formatter(FormatStrFormatter("%4.1g"))