Matplotlib 将值从数千缩小到 1

问题描述 投票:0回答:1

我从一个名为

car_data
的对象创建了一个 DataFrame:

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)

此后我尝试用

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')

这就是我得到的:

Matplotlib plot

这是奇怪的行为,因为正如您在上面的

car_data
字典中所看到的,它包含以千为单位的价格。该图以小数形式显示价格!

相反,如果我打印这个:

car_df['Price'][0]

它按预期返回

20000

注意:在我添加里程之前,这不会发生。

python dataframe matplotlib plot
1个回答
1
投票

不,散点图中的价格是 0-3 百万。仔细观察轴。

如果您想避免,可以添加


ax.get_xaxis().get_major_formatter().set_scientific(False)

enter image description here

或者您可以设置自己的格式化程序

from matplotlib.ticker import FormatStrFormatter
ax.xaxis.set_major_formatter(FormatStrFormatter("%4.1g"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.