assuming that I receive multiple dataframes sequentially from some tasks that I perform, how do I create regplots that will show up together - something similar to what's shown below.
assuming these are my codes which generate dfs with random numbers. I would like to show those 4 regplots together
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
sns.set()
for i in range(4):
df1 = pd.DataFrame(np.random.randint(1,10,10),columns=['A'])
df2 = pd.DataFrame(np.random.randint(1,10,10),columns=['B'])
df = pd.concat([df1,df2],axis=1)
sns.regplot(df1,df2)
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
fig, axs = plt.subplots(2, 2)
for ax, i in zip(axs.flat, range(4)):
df1 = pd.DataFrame(np.random.randint(1,10,10),columns=['A'])
df2 = pd.DataFrame(np.random.randint(1,10,10),columns=['B'])
df = pd.concat([df1,df2],axis=1)
sns.regplot(x='A', y='B', data=df, ax=ax)
plt.tight_layout()
plt.show()
Related
I would like to plot a line plot and make different overlay based on condition as illustrated below.
May I know how, or if possible, please kindly redirect me to right material on achieving the intended objective.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(0)
rng = np.random.default_rng(2)
mlist=[]
for _ in range(4):
m=np.random.rand(4).tolist()
n=rng.integers(0, 6, size=(1)).tolist()*4
df = pd.DataFrame(zip(m,n), columns=['yval','type'])
mlist.append(df)
df=pd.concat(mlist).reset_index(drop=True).reset_index()
sns.lineplot(data=df, x="index", y="yval")
plt.show()
Suggestion using Matplotlib or Seaborn, or any other package are welcome
The filling of the section was achieved using axvspan. I also used text for annotations.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(0)
rng = np.random.default_rng(2)
mlist=[]
for _ in range(4):
m=np.random.rand(4).tolist()
n=rng.integers(0, 6, size=(1)).tolist()*4
df = pd.DataFrame(zip(m,n), columns=['yval','type'])
mlist.append(df)
df=pd.concat(mlist).reset_index(drop=True).reset_index()
g = sns.lineplot(data=df, x="index", y="yval")
overlay = {0:'m',1:'gray',5:'r'}
for i in np.arange(0,len(df),4):
tmp = df.iloc[i:i+4, :]
v = overlay.get(tmp.type.unique()[0])
g.axvspan(min(tmp.index), max(tmp.index)+1, color=v, alpha=0.3)
g.text(((min(tmp.index)+max(tmp.index)+1) / 2)-1, 0.1,'type {}'.format(tmp.type.unique()[0]), fontsize=12)
plt.show()
Using Matplotlib add_patch and text
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as mcolors
# nn=mcolors.CSS4_COLORS
all_colors=list(mcolors.TABLEAU_COLORS.keys())
b=1
np.random.seed(0)
rng = np.random.default_rng(2)
mlist=[]
for _ in range(4):
m=np.random.rand(4).tolist()
n=rng.integers(0, 6, size=(1)).tolist()*4
df = pd.DataFrame(zip(m,n), columns=['yval','type'])
mlist.append(df)
df=pd.concat(mlist).reset_index(drop=True).reset_index()
# df.to_feather('test.feather')
# df=pd.read_feather('test.feather')
df['C'] = df['type'].diff()
df['C']=df['C'].fillna(10)
nb=df.type[(df['C'] != 0)].to_frame().reset_index()
unique_val=nb['type'].drop_duplicates().sort_values().tolist()
ngroup_type=dict(zip(unique_val,[f'type {idx}' for idx in unique_val]))
nb['ngroup']=nb["type"].map(ngroup_type)
color_group=all_colors[:len(unique_val)]
res = dict(zip(unique_val, color_group))
nb["color"] = nb["type"].map(res)
starting_point=nb["index"].values.tolist()
mcolor=nb["color"].values.tolist()
group_type=nb["ngroup"].values.tolist()
nspace=4
nheight=1
fg=sns.lineplot(data=df, x="index", y="yval")
for ncolor,spoint,gtype in zip(mcolor,starting_point,group_type):
fg.axes.add_patch(patches.Rectangle((spoint, 0),
nspace,nheight,edgecolor = 'blue',
facecolor = ncolor,fill=True,alpha=0.1,ls=':') )
fg.axes.text(spoint+1.5, 0.1, gtype , size=10,
va="baseline", ha="left", multialignment="left")
plt.show()
I am having some problems with plotting a Pandas dataframe with repeating range on x-axis after every 17 points. It doesn't start from new line after repetition. How to fix this issue.
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_excel('BS.xlsx')
plt.plot(df.BZ, df.energy)
plt.show()
Repeating Dataframe
Based on the df provided. You can try as below:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_excel('BS.xlsx')
df['range']= df.index//17
ax = plt.axes()
df.groupby('range').apply(lambda x:x.plot(x='BZ', y= 'energy', legend = False, ax=ax))
plt.show()
I am trying to plot group of bar charts. I was able to give different colors within each group but how to give different colors to different groups?
MWE
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({0: [10,20,80],
1: [20,40,60]},index=['a','b','c'])
df
# another test dataframe
df = pd.DataFrame({0: [10,20,80,10],
1: [20,40,60,70],
2: [20,40,60,70],
},
index=['a','b','c','d'])
pal = 'magma'
color=sns.color_palette(pal,len(df)) # list of rgb
fig, ax = plt.subplots()
df.plot.bar(ax=ax,color=color)
Output
Required
Here the variable color has three values, I want to use these three colors for three groups. For example group a now has two colors, I want it to have only one color.
Similar links
python assign different colors to bars in barplot
Here is a workaround using plt.bar()
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({0: [10,20,80],
1: [20,40,60],
'g':['a','b','c']})
pal = 'magma'
color=sns.color_palette(pal,len(df)) # list of rgb
fig, ax = plt.subplots()
width=.25
gb = df.groupby('g')
positions = range(len(gb))
for c, x, (_, group) in zip(color, positions, gb):
ax.bar(x-width/2, group[0], width, color=c, edgecolor='k')
ax.bar(x+width/2, group[1], width, color=c, edgecolor='k')
ax.set_xticks(positions)
ax.set_xticklabels(df['g'])
You can use axis.patches:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({0: [10,20,80,10],
1: [20,40,60,70],
2: [20,40,60,70],
},
index=['a','b','c','d'])
pal = 'magma'
color = sns.color_palette(pal,len(df)) # list of rgb
color = color * df.shape[1]
fig, ax = plt.subplots()
df.plot.bar(ax=ax)
ax.get_legend().remove()
for p,c in zip(ax.patches,color):
p.set_color(c)
Gives:
I'm trying to create a bar plot from a DataFrame with Datetime Index.
This is an example working code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set()
index = pd.date_range('2012-01-01', periods=48, freq='M')
data = np.random.randint(100, size = (len(index),1))
df = pd.DataFrame(index=index, data=data, columns=['numbers'])
fig, ax = plt.subplots()
ax.bar(df.index, df['numbers'])
The result is:
As you can see the white bars cannot be distinguished well with respect of the background (why?).
I tried using instead:
df['numbers'].plot(kind='bar')
import matplotlib.ticker as ticker
ticklabels = df.index.strftime('%Y-%m')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
with this result:
But in this way I lose the automatic xticks labels (and grid) 6-months spacing.
Any idea?
You can just change the style:
import matplotlib.pyplot as plt
index = pd.date_range('2012-01-01', periods=48, freq='M')
data = np.random.randint(100, size = (len(index),1))
df = pd.DataFrame(index=index, data=data, columns=['numbers'])
plt.figure(figsize=(12, 5))
plt.style.use('default')
plt.bar(df.index,df['numbers'],color="red")
You do not actually use seaborn. Replace ax.bar(df.index, df['numbers'])
with
sns.barplot(df.index, df['numbers'], ax=ax)
I would like create an plot with to display the last value on line. But i can not create the plot with the last value on chart. Do you have an idea for to resolve my problem, thanks you !
Input :
DataFrame
Plot
Output :
Cross = Last Value In columns
Output Final
# import eikon as ek
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import os
import seaborn as sns; sns.set()
import pylab
from scipy import *
from pylab import *
fichier = "P:/GESTION_RPSE/GES - Gestion Epargne Salariale/Dvp Python/Florian/Absolute
Performance/PLOT.csv"
df = pd.read_csv(fichier)
df = df.drop(columns=['Unnamed: 0'])
# sns.set()
plt.figure(figsize=(16, 10))
df = df.melt('Date', var_name='Company', value_name='Value')
#palette = sns.color_palette("husl",12)
ax = sns.lineplot(x="Date", y="Value", hue='Company', data=df).set_title("LaLaLa")
plt.show()
Do you just want to put an 'X' at the end of your lines?
If so, you could pass markerevery=[-1] to the call to lineplot(). However there are a few caveats:
You have to use style= instead of hue= otherwise, there are no markers drawn
Filled markers work better than unfilled markers (like "x"). You can just use markers=True to use the default markers, or pass a list markers=['s','d','o',etc...]
code:
fmri = sns.load_dataset("fmri")
fig, ax = plt.subplots()
ax = sns.lineplot(x="timepoint", y="signal",
style="event", data=fmri, ci=None, markers=True, markevery=[-1], markersize=10)