unable to obtain desired line graph through datafram.plot() - pandas

years = list(map(str,range(1980,2014)))
df_can.loc[['Haiti'],years].plot(kind='line')
plt.title('Immigration from Haiti')
plt.ylabel('Number of immigrants')
plt.xlabel('Years')
plt.show()
This is the plot I'm getting from above code
https://i.stack.imgur.com/nqM5F.png instead of line graph. I tried all different methods still not able to get the desired line graph.

I don't have your data, so I can't recreate the graph, but I'm pretty sure you're just missing a for loop.
years = list(map(str,range(1980,2014)))
for i in years:
df_can.loc[['Haiti'],i].plot(kind='line')
plt.title('Immigration from Haiti')
plt.ylabel('Number of immigrants')
plt.xlabel('Years')
plt.show()
Right now as your code stands you're trying to plot all of the years, and all of the data, on one line. It doesn't intuitively know to break it up.

To plot a line graph we need two columns of data but the above code as two rows one with immigrants data and the other with year from 1980-2013. So, we transpose this to get two columns and as the years are column names in string we convert them to integer data type.
years = list(map(str,range(1980,2014))
df_canada=df_can.loc[['Haiti'],years].plot(kind='line').transpose()
df_canada.index= df_canada.index.map(int)
plt.title('Immigration from Haiti')
plt.ylabel('Number of immigrants')
plt.xlabel('Years') plt.show()

Related

How to group a Box plot by the column names of a data frame in Seaborn? [duplicate]

This question already has answers here:
Boxplot of Multiple Columns of a Pandas Dataframe on the Same Figure (seaborn)
(4 answers)
Closed 24 days ago.
I'm a beginner trying to learn data science and this is my first time using the seaborn and matplotlib libraries. I have this practice dataset and data frame :
that I want to turn into a box plot and I want the x-axis to have all of the column names and the y-axis to range from 0 - 700 but, I'm not sure what to do.
I tried using : random_variable = sms.boxplot(data = df, x = ?, y = 'TAX')
which does give me the y-axis that is close to what I am looking for but, I don't know what the x-axis should be set equal too.
I thought may I could use the keys of the dataframe but, all I got was this mess that doesn't work:
random_variable = sms.boxplot(x = df.keys(), y = df['TAX'])
I want it to look like this but, I'm really lost on how to do this:
I apologize if this is an easy fix but, I would appreciate any help.
If you just want to display your data like that go with
import seaborn as sns
sns.boxplot(data=df)

Annotating numeric values on grouped bars chart in pyplot

Good evening all,
I have a pd.dataframe called plot_eigen_vecs_df which is of (3,11) dimension, and I am plotting each column value grouped by rows on a bar chart. I am using the following code:
plot_eigen_vecs_df.plot(kind='bar', figsize=(12, 8),
title='First 3 PCs factor loadings',
xlabel='Evects', legend=True)
The result is this graph:
enter image description here
I would like to keep the graph (grouped) exactly as it is, but I need to show the numeric value above each bars.
Thank you
I tried the add_label method, but unfortunately I am currently using a version of pyplot which is not the most recent, so .add_label doesn't work for me. Could you please help on the matter?

Why stackplot and pandas area plot show stacked area so different?

The pandas dataframe df is like below from month Jan to Oct.
First graph was drawn by the command plt.stackplot(df.index,df, colors=pal, alpha=0.4) and second graph was drawn by command df.plot(kind='area', stacked=True, figsize=(18, 10)).
The second one is what I expected to see and I believe is also correct. But why the two graphs are so different from the same dataset? How can I fix the first command to correct the first graph?

Plot data using facet-grid in seaborn [duplicate]

This question already has answers here:
How to change the number or rows and columns in my catplot
(2 answers)
Seaborn multiple barplots
(2 answers)
subplotting with catplot
(1 answer)
Closed 4 months ago.
I have this dataset table
And i want to plot profit made by different sub_category in different region.
now i am using this code to make a plot using seaborn
sns.barplot(data=sub_category_profit,x="sub_category",y="profit",hue="region")
I am getting a extreamly huge plot like this output
is there is any way i can get sub-plots of this like a facet-gird. Like subplots of different sub_category. I have used the facet grid function but it is the also not working properly.
g=sns.FacetGrid(data=sub_category_profit,col="sub_category")
g.map(sns.barplot(data=sub_category_profit,x="region",y="profit"))
I am getting the following output
As you can see in the facet grid output the plots are very small and the bar graph is just present on one grid.
See docs on seaborn.FacetGrid, particularly the posted example, where you should not pass the data again in the map call but simply the plot function and x and y variables to draw plots to corresponding facets.
Also, consider the col_wrap argument since you do not specify row to avoid the very wide plot output.
g=sns.FacetGrid(data=sub_category_profit, col="sub_category", col_wrap=4)
g.map_dataframe(sns.barplot, x="region", y="profit")

matplot pandas plotting multiple y values on the same column

Trying to plot using matplot but lines based on the value of a non x , y column.
For example this is my DF:
code reqs value
AGB 253319 57010.16528
ABC 242292 35660.58176
DCC 240440 36587.45336
CHB 172441 57825.83052
DEF 148357 34129.71166
Which yields this plot df.plot(x='reqs',y='value',figsize=(8,4)) :
What I'm looking to do is have a plot with multiple lines one line for each of the codes. Right now its just doing 1 line and ignoring the code column.
I tried searching for an answer but each one is asking for multiple y's I dont have multiple y's I have the same y but with different focuses
(surely i'm using the wrong terms to describe what I'm trying to do hopefully this example and image makes sense)
The result should look something like this:
So I worked out how to do exactly ^ if anyone is curious:
plt_df = df
fig, ax = plt.subplots()
for key,grp in plt_df.groupby(['code']):
ax = grp.plot(ax=ax, kind ='line',x='reqs',y='value',label=key,figsize=(20,4),title = "someTitle")
plt.show()