factorplot with seaborn python - python

I try to plot a factorplot with seaborn
Here is my python code :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import style
import pandas as pd
import seaborn as sns
import io
style.use('ggplot')
#load dataset into df2 dataframe
df2 = pd.read_csv('C:/Users/Demonstrator/Downloads/power.csv',delimiter=';')
#drop NaN rows from df2 to build df_no_missing
df_no_missing = df2.dropna().copy()
df_no_missing.head()
df_no_missing['depassement'] = np.where((df_no_missing['P_ACT_KW'] - df_no_missing['P_SOUSCR']) < 0, 0, df_no_missing['P_ACT_KW'] - df_no_missing['P_SOUSCR'])
#build a factorplot
sns.factorplot(data=df_no_missing, x="TIMESTAMP", y="P_ACT_KW")
It is impossible to view a result the process seems busy.

Related

Unable to change color of plotted lines on matplotlib

Referred to this SO post here: Matplotlib set_color_cycle versus set_prop_cycle
But I was unable to set all 20 lines' colors to be different, picture of the graph here:
Here is my code:
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from cycler import cycler
df = pd.read_csv(r'data.csv', index_col="Date", parse_dates=True)
df.rolling(window=30).max()[30:].head(20)
ax = df.plot()
ax.set(title='Qingdao Port', ylabel='Monthly Average Prices')
ax.set_prop_cycle('color',plt.cm.jet(np.linspace(0,1,20)))
plt.show()
Do help me out here!
User DavidG has helped me with the issue, for future reference the updated code will be included here:
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from cycler import cycler
fig, ax = plt.subplots()
ax.set_prop_cycle('color',plt.cm.tab20(np.linspace(0,1,20)))
df = pd.read_csv(r'data.csv', index_col="Date", parse_dates=True)
df.rolling(window=30).max()[30:].head(20)
df.plot(ax=ax)
ax.set(title='Qingdao Port', ylabel='Monthly Average Prices')
plt.show()

Axis not displaying in Matplotlib

import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline
!wget -O FuelConsumption.csv https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-
data/CognitiveClass/ML0101ENv3/labs/FuelConsumptionCo2.csv
df = pd.read_csv("FuelConsumption.csv")
df.head()
df1 = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
df1.head(9)
plt.scatter(df1.ENGINESIZE , df1.CO2EMISSIONS , color = "black")
plt.xlabel=("enginesize")
plt.ylabel=("emission")
plt.show()
when i try to run this code i get scatter graph without axis label.
how can i get axis label if anyone can assist me ?
You need to pass the label names to xlabel and ylabel.
If it's throwing error - TypeError: 'str' object is not callable then restart the ipython kernel
import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline
df = pd.read_csv("FuelConsumption.csv")
df.head()
df1 = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
df1.head(9)
plt.scatter(df1.ENGINESIZE , df1.CO2EMISSIONS , color = "black")
plt.xlabel("enginesize")
plt.ylabel("emission")
plt.show()

How to pipe plotly line plot to pandas dataframe?

How can we use .pipe() to get plotly express line plot?
Code
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
df = pd.DataFrame(data={'count':np.random.randint(1,20,10)},
index=pd.date_range('2020-01-01','2020-01-10')
)
Line plot (this works)
df1 = df.resample('2D').sum()
px.line(df1,x=df1.index,y='count')
Using pipe
Here creating df1 is un-necessary. How can we use pipe?
My attempt
df.resample('2D').sum().pipe(px.line,x=lambda x: x.index,y='count')
# this does not work, gives empty plot
How to get the correct image?
You are close:
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
df = pd.DataFrame(data={'count':np.random.randint(1,20,10)},
index=pd.date_range('2020-01-01','2020-01-10')
)
# line plot with pipe
df.resample('2D').sum().pipe(lambda ts:
px.line(ts, x=ts.index,y='count'))
Output:

why my ipython notebook didn't show result?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
%pylab inline
pylab.rcParams['figure.figsize'] = (12, 12)
import seaborn as sns
all_sets=pd.read_csv(r"C:\Users\champion\Desktop\ch02\AllSets-x.json")
df_list=[]
for i in all_sets.columns.values:
deck=allset[str(i)]["card"]
df = pd.DataFrame(deck)
df_list.append(df)
all_cards = pd.concat(df_list)
all_cards.cmc.fillna(0)
all_cards.reset_index(inplace=True)
all_cards.columns.values
when I exceuted my code,result didn't show.
And ipython notebook have IN[*] in right.

pandas plot - multiple colorbars

How can I properly plot 2 colorbars with pandas scatter plots? Right now the first colorbar is duplicated:
https://gist.github.com/denfromufa/45c446690a69265d39dd
import numpy as np
import pandas as pd
df=pd.DataFrame(np.random.random([100,5]),columns='A B C D E'.split())
df.head()
%matplotlib inline
ax1=df.plot(kind='scatter',x='A',y='B',c='C',s=df.D*50,cmap='summer',linewidth=0,sharex=False);
df.plot(ax=ax1,kind='scatter',x='A',y='C',c='D',s=df.B*50,cmap='winter',linewidth=0,sharex=False);
You can use the matplotlib functions directly:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(np.random.random([100,5]),columns='A B C D E'.split())
sc1 = plt.scatter(x=df['A'], y=df['B'], c=df['C'], s=50*df['D'], cmap='summer')
plt.colorbar(sc1)
sc2 = plt.scatter(x=df['A'], y=df['C'], c=df['D'], s=50*df['B'], cmap='winter')
plt.colorbar(sc2)
which produces

Categories

Resources