Matplotlib Label issue - python

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

Related

How to adjust the x labels on seasonal_decompose()

I'm trying to rotate and reduce the x label on seasonal_decompose(),
from statsmodels.tsa.seasonal. However, I only got the from the last chart to change. How can I edit all of them?
Please help me.
model_series = tsa.seasonal_decompose(y, model='additive')
fig = model_series.plot()
plt.xticks(fontsize=7)
# plt.show()

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')

Facet box plots by additional category in Python

I have created a box plot in Python with the Seaborn package (sns) as shown in the code below where the x-axis is age ranges and the y-axis is a dollar value number ('AveMonthSpend').
def plot_box(df, col, col_y ='AveMonthSpend'):
sns.set_style("whitegrid")
sns.boxplot(col, col_y, data=df)
plt.xlabel(col) # Set text for the x axis
plt.ylabel(col_y)# Set text for y axis
plt.show()
plot_box(df_3, 'age_range')
What I would like to do is facet this box plot by a third column - 'Gender' with the two values being ['M', 'F']. I have read this site and have tried many options, but none seem to work.
Click here for image of box plots
Following is one of the options I tried:
g = sns.FacetGrid(pd.melt(df, id_vars='Gender'), col='Gender')
g.map(sns.boxplot, 'age_range', 'AveMonthSpend')
For this I got the following error:
KeyError: "['age_range' 'AveMonthSpend'] not in index"
Any suggestions would be greatly appreciated. Thank you!

Convert a boxplot to a scatter plot in seaborn

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")

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