我从一个名为
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)
的对象创建了一个DataFrame,之后我尝试用
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')
绘制它,这就是我得到的:
这是奇怪的行为,因为,正如你可以在上面的
car_data
字典中看到,它包含以千为单位的价格。该图将价格显示为小数!
相反,如果我打印此内容:
car_df['Price'][0]
它会按预期返回
20000
注意:在我添加里程之前,这不会发生。
在图中看到的值没有错误。正在创建散点图,其中每个点的 x 坐标由
car_df['Price']
列给出,y 坐标由
car_df['Year']
列给出。查看的数据,汽车的价格在 18,000 到 3,000,000 之间,年份在 2017 到 2022 之间。创建的图准确地反映了这一点,x 轴上的刻度从 0 到 3,000,000,y 轴上的刻度从 2017 到 2022。
但是,由于值范围很大,因此 x 轴上的刻度标签格式化为科学记数法。在 matplotlib 中,当轴的限制在 1e3 到 1e6 之间时,通常使用科学记数法来表示大数。在这种情况下,x 轴标签显示为
0.0e6
、
0.5e6
、
1.0e6
等,这分别代表 0、500,000、1,000,000 等。
如果要更改 x 轴刻度标签的格式,可以使用
matplotlib.ticker.ScalarFormatter
类。以下是如何操作:
import matplotlib.ticker as ticker
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')
# 将 x 轴刻度标签格式更改为普通数字格式
ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True))
plt.show()
这会强制 x 轴以非科学记数法显示刻度标签,从而更容易理解价格。
此外,的观察结果“ 注意:在我添加里程之前,这不会发生。 ”是不正确的。造成这种现象的原因只是价格和年份数据值的范围,这与是否包含“里程”列无关。
标签:python,dataframe,matplotlib,plot From: 78117038