Seaborn factorplot - python-3.x

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

Related

Cannot configure matplotlib rcparams for plot color

I am not able to change default color from blue to red using plt.rc() or mpl.rcparams
According to Matplotlib's official documentation
Here is the code I tried:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
data = np.random.randn(50)
mpl.rcParams['lines.color'] = 'r'
plt.plot(data)
Output:
If you are using a relatively new matplotlib version (i.e. 1.5+) you should use axes.prop_cycleas described here.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
data = np.random.randn(50)
import cycler
plt.rcParams['axes.prop_cycle'] = cycler.cycler(color='r')
plt.plot(data)

Python exponential plot is wrong

I am new using python and try to do some plots. I realized, that a plot of a bump function is incorrect. I have no idea how python came to this result.
This is my 'code'
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
class MainBody():
x = np.linspace(0.0001,99.9999,1000)
result = np.exp((-1.0)/(x*(100.0-x)))
plt.plot(x,result)
plt.show()
I got this result
but I should get this
I know that Python is powerful but I think such simple things should work without occuring such errors, where is my mistake?
Thank you
Matthias
Use plt.ylim to set the y-limits. Otherwise, by default, matplotlib will try to show the entire dataset, whose y-limits go roughly from 0 to 1:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.0001,99.9999,1000)
result = np.exp((-1.0)/(x*(100.0-x)))
plt.plot(x,result)
plt.ylim(0.9975, 0.9999)
plt.show()

I was visualizing a data set using seaborn in python3 but its giving me an error. unsupported operand type(s) for /: 'str' and 'int'

import pandas as pd
from pandas import Series,DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
poll_df=pd.read_csv('http://elections.huffingtonpost.com/pollster/2012-general-election-romney-vs-obama.csv')
#poll_df is the data which i have read from a csv file.
sns.factorplot('Affiliation',data=poll_df)
I have difficulty understanding the question. Column Affiliation has a str value not numeric.
if you want to count total number of each str category and have a bar plot try:
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
poll_df=pd.read_csv('http://elections.huffingtonpost.com/pollster/2012-general-election-romney-vs-obama.csv')
#poll_df is the data which i have read from a csv file.
sns.countplot('Affiliation',data=poll_df)
alternatively upload the image of what kind of plot you would like to have as a result

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

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