Unable to view Seaborn Plots - python-3.x

I am trying to create a plot using seaborn with data from an excel sheet on python on Jupiter.
The code I am using is:
[In]: import numpy as np
[In]:import pandas as pd
[In]:import seaborn as sns
[In]:import matplotlid.pyplot as plt
[In]:%matplotlib
[In]: df =pd.read_excel(r'C:\Users\jcree\OneDrive\Documents\Accounting\Applied_Accounting_Principles\Exercises\Data_Analytics\Workbook1.xlsx.xlsx')
[In]: sns.distplot(df['Item Price'])
This is where the plot should show up but nothing is happening.
I tried using this solution Seaborn plots not showing up but I am unableto get a chart to show.
Any help would be appreciated

Related

How to plot frequency from a CSV Column in Python

I have a CSV file with columns: created_at, hashtags, media, urls, favorite_count.
I would like to plot the frequency of hashtags.
To read the CSV file I used pandas (but I would like also to show/list the result):
import pandas as pd
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('/path/file',delimiter=",")
Then, to plot the frequency of hashtags in the file, I used
plt.plot(df["hashtags"])
plt.show()
but I received the error: "nan is not a string".
Any suggestion on how to plot the column and visualise the results as both plot and pretty table?
Thanks
You can try this:
df.dropna()
df.reset_index(drop = True)
plt.plot(df["Column1"], df["Column1"])
plt.show()

How to plot a function with the pandas library using Python 3

Please, I need some help, so I am still in the learning phase of the Python programming for Data Science. I tried plotting a function but I am getting this. Please, what can I do? I am using Python 3. I appreciate your insights, explanations.
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sb
mtcars = pd.read_csv('mtcars.csv')
mtcars.columns = ['Car names','mpg','cyl','disp','hp','drat','wt','qsec','vs','am','gear','carb']
mpg1 = mtcars['mpg']
print(mpg1.plot())
df = mtcars[['mpg','cyl','wt']]
print(df.plot())
When I run to see what the plot is like, this is what I get:
AxesSubplot(0.125,0.11;0.775x0.77)
Please can you explain? How can I see the plot?
Thank you so much
you shouldn't "print" the plot, instead you should just call mpg1.plot() (eventually follpwed by plt.show() if needed)

How to plot multiple line graph for a column value from a CSV file?

I tried to plot a graph for energies of 4 nodes using line graph but I'm not able to identify which line represent which node ID(1,2,3 or 4)
My csv looks something like this :
Time,Source,Destination,Bits,Energy
0,1,2,288,9.9999856
1058,1,2,288,9.9999856
1140,2,1,96,9.9999808
1958,2,3,96,9.9999952
2024,2,1,96,9.9999808
2051,2,3,288,9.999966399
3063,2,3,288,9.9999808
3126,3,2,96,9.999976
3127,2,1,288,9.9999664
3946,3,2,96,9.999961599
8340,1,2,288,9.999952
9418,1,2,288,9.999947199
9479,2,1,96,9.999942399
10299,2,3,96,9.9999712
10365,2,1,96,9.9999472
10758,2,3,288,9.999927999
11770,2,3,288,9.9999568
11832,3,2,96,9.999951999
11842,2,1,288,9.9999328
Code :
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('DS1.csv')
for Energy,data in df.groupby('Source'):
plt.plot(data['Time'], data['Energy'])
plt.legend(data['Source'])
#print(data)
plt.xlabel('Time')
plt.ylabel('Energy')
plt.legend()
plt.show()
I actually want to plot source,energy vs Time for all sources(1 to 4)
You need to set the label.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('DS1.csv')
for Energy, data in df.groupby('Source'):
plt.plot(data['Time'], data['Energy'], label=Energy)
#print(data)
plt.xlabel('Time')
plt.ylabel('Energy')
plt.legend()
plt.show()

Seaborn factorplot

I am trying to create a factor plot but I am not able to change the kind of it from point to bar. How do we do that?
The codes used are
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.catplot('Sex',kind="bar",data=titanic_df)
The seaborn documentation has the exact example you are looking for. Following the documentation, if you run the below lines, it should generate the bar plot shown.
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
titanic = sns.load_dataset("titanic")
exercise = sns.load_dataset("exercise")
g = sns.catplot("alive", col="deck",
col_wrap=3, data=titanic[titanic.deck.notnull()],
kind="count", height=2.5, aspect=.8)
The important argument to note here is kind="count".

control seaborn regplot confidence intervals translucency

Anyone has a way to control the degree of translucency of the confidence intervals in seaborn regplot?
It's been bugging me (especially for black background plots) for a while and I still could not find anything on that topic.
import pandas as pd
import seaborn as sns
data = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])
sns.regplot('x', 'y', data=data)
You can set the alpha for the confidence interval band using matplotlib.pyplot.setp like:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])
ax = sns.regplot('x', 'y', data=data)
plt.setp(ax.collections[1], alpha=0.2)
Just for reference, If you want to look at elements of a seaborn plot you can use ax.get_children().

Resources