control seaborn regplot confidence intervals translucency - python-3.x

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().

Related

How to set x ticks for seaborn (python) line plot

I made a line plot chart for years 1960-2014 using seaborn but the xticks aren't correct. I want only intervals to appear (like 1960, 1970, 1980, etc).How do i adjust the xticks? I tried rotating it but it didn't seem to work. Here is my code:
#plot figure using sns
g=sns.relplot(x="Year", y="Indicator_Value",
data=Emissions_C_df,
kind="line",
style="Indicator_Name",
hue="Indicator_Name",
)
plt.show()
You can use a MaxNLocator from matplotlib.ticker for the major ticks (decades) and manually set specific minor ticks with a FixedLocator.
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, MaxNLocator
a = np.arange(50)
d = {'Year': 1953+a, 'Indicator_Value': a}
df = pd.DataFrame(data=d)
g = sns.relplot(x="Year", y="Indicator_Value",
data=df,
kind="line")
ax = plt.gca()
ax.xaxis.set_major_locator(MaxNLocator(steps=[10]))
ax.xaxis.set_minor_locator(FixedLocator(range(1960,1970)))
plt.show()

How can I fix this error when trying to hide y axis labels on matplotlib axis

I'm attempting to hide the y-axis labels on a matplotlib axis. There seems to be an error that some of the labels are hidden but others are not. Is this a bug and is there a way to turn off all the labels? Is it the way that I'm plotting?
Note: I'm only showing half my plot the other half is a cartopy map.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io.img_tiles import Stamen
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature
import os
import pickle
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from pathlib import Path
fig=plt.figure(figsize=(16,9), dpi=300, constrained_layout=True)
gs0=fig.add_gridspec(1,2)
gs00=gs0[0].subgridspec(10,1)
gs01=gs0[1].subgridspec(1,1)
data_dir='/data/files/'
ext_=f'*.pkl'
p=Path(data_dir).glob(ext_)
s=[str(i) for i in p]
for i,j in enumerate(s):
ax=fig.add_subplot(gs00[i,0])
f=pickle.load(open(j, 'rb'))
ax.loglog(f[0], f[2])
ax.margins(x=0)
ax.set_yticklabels([])
ax2=fig.add_subplot(gs01[0,0], projection=ccrs.PlateCarree())
I think you can use plt.tick_params()
See explainations from this topic Remove xticks in a matplotlib plot?
Modification for y-axis:
from matplotlib import pyplot as plt
plt.plot(range(10))
plt.tick_params(
axis='y', # changes apply to the y-axis
which='both', # both major and minor ticks are affected
left=False, # ticks along the left edge are off
labelleft=False) # labels along the left edge are off
plt.show()

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

add a line to matplotlib subplots

I would like to do a subplot of two figures with matplotlib and add a horizontal line in both. This is probably basic, but I don't know how to specify that one of the lines should be drawn in the first figure, they both end up in the last one. e.g.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
s1= pd.Series(np.random.rand(10))
s2= pd.Series(np.random.rand(10))
fig, axes = plt.subplots(nrows=2,ncols=1)
f1= s1.plot(ax=axes[0])
l1=plt.axhline(0.5,color='black',ls='--')
l1.set_label('l1')
f2= s1.plot(ax=axes[1])
l2=plt.axhline(0.7,color='red',ls='--')
l2.set_label('l2')
plt.legend()
axhline does not have "ax" as an argument, as the pandas plot function does. So this would work:
l1=plt.axhline(0.5,color='black',ls='--',ax=axes[0])
I read the examples in matplotlib and I tried with this other option that does not work either (probably for good reasons)
axes[0].plt.axhline(0.5,color='black',ls='--')
How should I do to draw lines in subplots? Ideally with a legend Thanks!
with the help of #Nick Becker I answered my own "syntax" question.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
s1= pd.Series(np.random.rand(10))
s2= pd.Series(np.random.randn(10))
fig, axes = plt.subplots(nrows=2,ncols=1)
f1= s1.plot(ax=axes[0],label='s1')
l1=axes[0].axhline(0.5,color='black',ls='--')
l1.set_label('l1')
axes[0].legend(loc='best')
f2= s1.plot(ax=axes[1],label='s2')
l2=axes[1].axhline(0.5,color='black',ls='--')
l2.set_label('l2')
axes[1].legend(loc='best')

Resources