Python exponential plot is wrong - python-3.x

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

Related

f() missing 1 required positional argument: 't'

I just tried to execute this code. but it shows always this error:'f() missing 1 required positional argument:'t'
please can you tell me what should I change?
import numpy as np
def f(y,z,t):
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
sol=odeint(f,[0,1],t)
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
Depending on what you are trying to do, you can get around it two ways. To pass in additional arguments other than y and t you need to include them as a constant in the function parameter.
import numpy as np
def f(t,y,z):
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
z = 10.0
sol=odeint(f,[0,1],t, tfirst=True, args=(z, ))
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
This will still cause an error. However, if you are also trying to obtain z as result then you should be able to run:
import numpy as np
def f(t,inp):
y, z = inp
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
sol=odeint(f,[0,1],t, tfirst=True)
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
This should run without any errors but you may need to double check that this is the result you are expecting.
(The tfirst argument is just for clarity to make ensure the order of arguments provided is correct, you can remove and re-order if you want as well.)
Documentation for odeint function here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html

How do I make my plot look like this with matplotlib?

So right now I'm trying to simulate a Poisson process for an assignment, here's the code so far:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
y = np.arange(0,21,1)
x = np.cumsum(np.random.exponential(2,21))
print(y)
print(x)
sns.set()
plt.plot(x,y)
plt.show()
The problem arises when I try plotting it. The code above, as expected, produces a normal matplotlib plot that looks like this:
However I need it to look like this:
Is there an easy way of doing it? I tried messing with bar plots but was unable to produce something that looks good.
The graph that you are wanting to plot is called as step plot in matplotlib. In order to plot it replace plt.plot(x,y) with plt.step(x,y)
So, your code becomes:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
y = np.arange(0,21,1)
x = np.cumsum(np.random.exponential(2,21))
print(y)
print(x)
sns.set()
plt.step(x,y)
plt.show()

Matplotlib.plot not found

I installed Matplotlib via Anaconda from here: https://anaconda.org/conda-forge/matplotlib
I used the very first command in Anaconda prompt.
But when I tried to plot from python (Spyder) as the following, I get the message:
ModuleNotFoundError: No module named 'matplotlib.plot'
import numpy as np
import matplotlib.plot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
I have installed numpy, pandas and such using the same method and they work well.
How can I fix this?
Thank you so much.
matplotlib.pyplot is a state-based interface to matplotlib. pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation. Therefore, whenever trying to work with graphs and what is commonly known and informally often referred as matplotlib you should import matplotlib.pyplot as plt:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)

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)

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

Resources