0

I create the following plots within a for loop:

cat_var={}
categories=['symboling','fuel-type','make']

for x in categories:
    cat_var[x]=df[x].unique()

for key in cat_var.keys():
    plt.figure(figsize=(10,10))
    for value in cat_var[key]:
        subset= df[df[key] == value]
        sns.distplot(subset['price'], hist = False, kde = True,kde_kws = {'linewidth': 3},label = value) 
    plt.legend(prop={'size': 16}, title = key)
    plt.title('Price distribution per %s level'% key)
    plt.xlabel('Price')
    plt.ylabel('Density')
    plt.show()

It is working, but I would like to plot them so they appear all on the same row, whereas now I have them all on the same column. I was thinking about using something like:

fig, axes = plt.subplots(1, 3,figsize=(20,20))

but this does not produce the desired output wherever I put this last line of code.

Any thoughts?

1 Answer 1

1

If you want to have three plots on the same figure, then you need to use subplots and specify where each plot goes

fig, ax = plt.subplots(1, 3,figsize=(20,20))
for idx, key in enumerate(cat_var):       
    for value in cat_var[key]:
        subset= df[df[key] == value]
        sns.distplot(subset['price'], hist = False, ax= ax[idx], kde = True,kde_kws = {'linewidth': 3},label = value)  
plt.show()

With the following output:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

it's really hard to provide a working solution without your data, but it is a good starting point :)
Here is where the data can be found: archive.ics.uci.edu/ml/machine-learning-databases/autos/… you are right I should have included that in my question. The solution is not really working yet, but that is definitely a good start.
where are the headers? your data is non labeled hence the column syntax you're using can't be used on the raw data
I just tried my code with dummy column names and it worked
that's because plt is used when you're doing a simple plot. When you're doing subplots you need to use ax[idx].set_title('your title')
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.