OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Matplotlib shrinking Value of "Price"

  • Thread starter Thread starter John Carter
  • Start date Start date
J

John Carter

Guest
I have created a DataFrame from car_data:

Code:
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)

Then I created a plot from the DF like this:

Code:
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 the output:

Matplotlib plot

Why doesn't it show the thousands for price? When I print out this line:

Code:
car_df['Price'][0]

It returns 20000 as expected.

Note: This didn't happen before I added Mileage.
<p>I have created a DataFrame from <code>car_data</code>:</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<p>Then I created a plot from the DF like this:</p>
<pre class="lang-py prettyprint-override"><code>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')
</code></pre>
<p>and the output:</p>
<p><a href="https://i.sstatic.net/6NNs1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6NNs1.png" alt="Matplotlib plot" /></a></p>
<p>Why doesn't it show the thousands for price? When I print out this line:</p>
<pre><code>car_df['Price'][0]
</code></pre>
<p>It returns <code>20000</code> as expected.</p>
<p><em>Note: This didn't happen before I added Mileage.</em></p>
 
Top