python reader graph gives a blank canvass

Mtcars is a public dataset in R. I’m not sure it’s a public dataset in python.

mtcars <- mtcars

I created this boxplot in R and part of what I’m doing is reordering the y-axis with the reorder() function.

ggplot(mtcars, aes(x = mpg, y = reorder(origin, mpg), color = origin)) +
  geom_boxplot() + 
  theme(legend.position = "none") +
  labs(title = "Mtcars", subtitle = "Box Plot") +
  theme(plot.title = element_text(face = "bold")) +
  ylab("country")

python reader graph gives a blank canvass

Now in python I have this boxplot that I created with seaborn:

plt.close()

seaborn.boxplot(x="mpg", y="origin", data=mtcars)
plt.suptitle("Mtcars", x=0.125, y=0.97, ha='left', fontweight = 'bold')
plt.title("boxplot", loc = 'left')
plt.show()

python reader graph gives a blank canvass

I’m trying to render it now but the same kind of treatment for R doesn’t work.

plt.close()

seaborn.boxplot(x="mpg", y=reorder("origin", 'mpg'), data=mtcars)
plt.suptitle("Mtcars", x=0.125, y=0.97, ha='left', fontweight = 'bold')
plt.title("boxplot", loc = 'left')
plt.show()

python reader graph gives a blank canvass

It’s not surprising it doesn’t work because it’s a different language; I do know that! But how would I do this reordering in python using Seaborn? I’m having trouble understanding if this is even part of the plotting process.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can compute a custom order and feed it to seaborn’s boxplot order parameter:

import seaborn as sns

mtcars = sns.load_dataset('mpg')

order = mtcars.groupby('origin')['mpg'].median().sort_values(ascending=False)

sns.boxplot(x="mpg", y="origin", data=mtcars, order=order.index)

plt.suptitle("Mtcars", x=0.125, y=0.97, ha='left', fontweight = 'bold')
plt.title("boxplot", loc = 'left')
plt.show()

NB. order also acts like a filter, so if values are missing, of non-existent they will be omitted in the graph

output:

python reader graph gives a blank canvass


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x