Convert a boxplot to a scatter plot in seaborn - python

I currently have a box plot which plots cgpa vs totalScore for the subject 'Mathematics'. Here is the code for the box plot:
a = sns.boxplot(data=masterdata[masterdata.courseName == "Mathematics"], x = "totalScore", y="cgpa")
How can I transform the box plot I have to a scatter plot? I don't know how to select the specific subject of 'Mathematics' in the scatter plot. Scatter plots just have x-label, y-label and hue. But how do I plot for "Mathematics" only, like I did in the box plot I have? Thank you in advance for your help.

According to here https://seaborn.pydata.org/generated/seaborn.regplot.html scatter plot also have a data parameter. so you should be able to do like you did before.
b = sns.regplot(data = masterdata[masterdata.courseName == "Mathematics"],x="totalscore", y="cgpa")

Related

Problems with pandas boxplot showing points on it

I am plotting a box plot with se following code:
plt.figure(figsize=(7,7))
plt.title("Title")
plt.ylabel('Y-ax')
boxplot = df.boxplot(grid=False, rot=90, fontsize=10)
plt.show()
And I get this plot:
Is there any way I can just show like the normal boxplot with the 50/75/90 percentiles and not those circles that I have no clue what do they mean?
The data frame is huge, maybe that is why these points are shown?

Matplotlib Label issue

I have dataframe and would like to plot the histogram for 2 variables. Below syntax gives the required plot however labels do not appear. Any help would appreciate.
colors = ['tan', 'lime']
labels = ['Score 1','Score 2']
plt.hist([test.score_1,test.score_2],bins = 10,histtype='bar',color=colors, label=labels)
Here is the plot
enter image description here

Shift matplotlib axes to match eachother

I am looking to overlay a scatter plot with a boxplot in matplotlib. I have created the chart but the x axes do not match--leading to the scatter plot showing dots that are shifted 1 tick to the left of the x axis for the boxplot. Below is my code.
fig, ax = plt.subplots()
ax.scatter(traits_5, data_df[traits_5].loc[y])
ax = greats_df[traits_5].boxplot ( showfliers=False , column=traits_5)
plt.ylabel ( 'Percentile Score' )
plt.title ( "Distribution of The Greats' Scores" )
ax.yaxis.set_major_formatter(mtick.PercentFormatter(1))
plt.show ()
Is it possible that the error is coming from the two different methods of plotting the data? I use matplotlib to plot the scatter and pandas to plot the boxplot. Matplotlib was plotting the rows on the xaxis, whereas I wanted the columns to be plotted along the x axis.
See outputted image below from above code.
Hard to investigate without having access to data, but if you just translate the x coordinates of your scatter plot, it should work:
ax.scatter([x+1 for x in traits_5], data_df[traits_5].loc[y])

Subplot is not plotting the actual data, what should I do?

I have a data frame with a related salary to the major.
I am trying to create horizontal bar charts of the majors sorted by salary.
My code looks like this:
fig, ax = plt.subplots()
topTenMajor = df[['Major','Salary']].sort_values('Salary', ascending=False).set_index('Major')
topTenMajor.sort_values('Salary', ascending=True).plot.barh(figsize=(5,10))
ax.set_title('Majors by Salary')
ax.set_xlabel('Salary')
ax.set_ylabel('Majors')
However, my chart shows one emptly plots on top with title, x label and y label,
and then a horizontal barchart under the empty plots without title and labels.
Why is this happening?
Thanks for any help!
barh will plot in a new figure / axes by default.
Either you need to tell it to plot in the fig, ax you created before.
Or you can set title and labels in the active figure automatically created:
topTenMajor = df[['Major','Salary']].sort_values('Salary', ascending=False).set_index('Major')
topTenMajor.sort_values('Salary', ascending=True).plot.barh(figsize=(5,10))
plt.title('Majors by Salary')
plt.xlabel('Salary')
plt.ylabel('Majors')

How to adjust x axis labels in a seaborn plot?

I am trying to plot histogram similar to this:Actual plot
However, I am unable to customize the x axis labels similar to the above figure.
My seaborn plot looks something like this,
my plot
I want the same x-axis labels ranging from 0 to 25000 with equal interval of 5000. It would be great if anyone can guide me in the right direction?
Code for my figure:
sns.set_style('darkgrid')
kws = dict(linewidth=.3, edgecolor='k')
g = sns.FacetGrid(college, hue='Private',size=6, aspect=2, palette = 'coolwarm')
g = g.map(plt.hist, 'Outstate', bins=24,alpha = 0.7,**kws).add_legend()
You should be able to do that by simply add:
plt.xticks(np.linspace(start=0, stop=25000, num=6))

Categories

Resources