Wentao Yang
Wentao Yang

Reputation: 35

matplotlib overlapping yaxis

I want to create a graph for my data frame. However, I got something like this: enter image description here

Code:

def Draw_RoomType_vs_Price():
    plt.figure(figsize=(9,6))
    plt.scatter(x=df['room_type'], y=df['price'])
    
    plt.title('Room Type vs Price', size = 15, weight='bold')
    plt.xlabel('Room Type', size = 12)
    plt.ylabel('Price', size = 12)
    plt.tight_layout()
    plt.show()
    
Draw_RoomType_vs_Price()

Dataframe: enter image description here

Datatype: enter image description here

Upvotes: 0

Views: 63

Answers (1)

SoheilStar
SoheilStar

Reputation: 434

updated!

I guess the below code will work, but as I didn't have the data I am not fully sure.

def Draw_RoomType_vs_Price():
    plt.figure(figsize=(9, 6))
    plt.scatter(x=df['room_type'], y=df['price'].apply(lambda x: float(x.replace('$', ''))))

    plt.title('Room Type vs Price', size=15, weight='bold')
    plt.xlabel('Room Type', size=12)
    plt.ylabel('Price', size=12)
    plt.tight_layout()
    plt.show()

=============================================

it seems the df['price'] type is string instead of int/float. you need to first remove other chars except for numbers by using a regex like:

import re
re.sub("[^0-9]", "", "$102")

then convert the result into int or float and you're ready to plot it.

Upvotes: 1

Related Questions