I'm relatively new to Python.
I'm trying to plot exponential term value ranging between 0 and 100.
There is no error indicated with the code but the Plot has no legend(line).
Kindly help me.
Thank you.
import math
import matplotlib.pyplot as plt
for i in range (0,101):
m = math.exp(i)
print("For i = ",i)
print("(e^i) = ",m)
plt.plot(i,m,'b--',linewidth=3)
plt.ylabel('e^i')
plt.xlabel('i')
plt.show()
You can change your code in this way:
import math
import matplotlib.pyplot as plt
l = list(range (0,101))
m_l = []
for i in l:
m = math.exp(i)
print("For i = ",i)
print("(e^i) = ",m)
m_l.append(m)
plt.plot(l,m_l,'b--',linewidth=3)
plt.ylabel('e^i')
plt.xlabel('i')
plt.legend(['m_l'])
plt.show()
With your code, you were plotting just the last values of i and m
Related
I made 30 random points using np.random.uniform.
And i want these points to be divided by random line like the picture below.
Could you give me some codes or advice??
Making points is easy but dividing is difficult ..
Code is like this
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import random
import math
m=np.random.uniform(-10,10,30)
n=np.random.uniform(-10,10,30)
a=random.randrange(-5,5)
b=random.randrange(-10,10)
x= np.array(range(-10,20))
y=a*x+b
plt.plot(x,y)
plt.scatter(m,n)
plt.xlim(-10,10)
plt.ylim(-10,10)
plt.show()
enter image description here
I want to devide points by the line like this
enter image description here
I feel like using for loop is possible but I don't know how. Could you give me some advice??
I think you can use the filter function to divide the points into two parts.
The code is Here.
import numpy as np
import matplotlib.pyplot as plt
import random
m=np.random.uniform(-10,10,30)
n=np.random.uniform(-10,10,30)
a=random.randrange(-5,5)
b=random.randrange(-10,10)
x= np.array(range(-10,20))
y=a*x+b
plt.plot(x,y)
# above
result = list(filter(lambda item: item[1] >= a*item[0]+b, zip(m,n)))
xa, ya = list(zip(*result))
plt.scatter(xa,ya,color = "r",marker="*")
# below
result = list(filter(lambda item: item[1] < a*item[0]+b, zip(m,n)))
xb, yb = list(zip(*result))
plt.scatter(xb,yb, marker="^",color="b")
plt.xlim(-10,10)
plt.ylim(-10,10)
plt.show()
I'm trying to print a logistic differential equation and I'm pretty sure the equation is written correctly but my graph doesn't display anything.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def eq(con,x):
return con*x*(1-x)
xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4
for num in range(len(xList)-1):
plt.plot(xList[num], eq(con,x))
x=eq(con,x)
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")
plt.show()
You get nothing in your plot because you are plotting points.
In plt you need to have x array and y array (that have the same length) in order to make a plot.
If you want to do exactly what you are doing I suggest to do like this:
import matplotlyb.pyplot as plt # just plt is sufficent
import numpy as np
def eq(con,x):
return con*x*(1-x)
xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4
y = np.zeros(len(xList)) # initialize an array with the same lenght as xList
for num in range(len(xList)-1):
y[num] = eq(con,x)
x=eq(con,x)
plt.figure() # A good habit is always to use figures in plt
plt.plot(xList, y) # 2 arrays of the same lenght
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")
plt.show() # now you should get somthing here
I hope that this helps you ^^
So let's say we have a random variable M and it's continuous. Its PDF function is (4x^3)/81 for 0≤x≤3 and 0 for outside of this interval. And let's say we also have another continuous random variable N that depends on M like N = M^2 + 3. How can I plot the joint CDF of M and N?
Stackoverflow won't let me type this joint CDF derivation in because it thinks it is improperly formatted code, but here is an image of it.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
m = np.linspace(0,3,100)
n = np.linspace(3,12,100)
m_mesh,n_mesh = np.meshgrid(m,n)
joint_cdf = np.minimum(m_mesh,np.sqrt(n_mesh-3))**4/81
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(m_mesh, n_mesh, joint_cdf, cmap=cm.coolwarm,linewidth=0, antialiased=False)
I want to plot a very simple histogram diagram with python. Here is my code :
from numpy import *
from matplotlib.pyplot import*
from random import*
nums = []
N = 10
for i in range(N):
a = randint(0,10)
nums.append(a)
bars= [0,1,2,3,4,5,6,7,8,9]
hist(nums)
show()
This is the result
How can I put the bars just in the integer place? Why does my diagram show the float numbers too?
you make bars but then don't use it. If you set the bins option of hist to bars, it all works fine
bars= [0,1,2,3,4,5,6,7,8,9]
hist(nums,bins=bars)
To set the yticks to only integer values, you can use a MultipleLocator from the matplotlib.ticker module:
from numpy import *
from matplotlib.pyplot import*
import matplotlib.ticker as ticker
from random import*
nums = []
N = 10
for i in range(N):
a = randint(0,10)
nums.append(a)
bars= [0,1,2,3,4,5,6,7,8,9]
hist(nums,bins=bars)
gca().yaxis.set_major_locator(ticker.MultipleLocator(1))
show()
So I made a program that does what I need, mainly plots histogram from my data, but I have a few issues with it:
Here's the program:
# -*- coding: cp1250 -*-
from __future__ import division
from numpy import *
from matplotlib import rc
from matplotlib.pyplot import *
import numpy as np
import matplotlib.pyplot as plt
data = loadtxt("mioni.txt", int)
nuz = len(data)
nsmp = 20
duz = int(nuz/nsmp)
L = []
for i1 in range(0,nsmp):
suma = 0
for i2 in range(0,duz):
suma += data[i1*duz+i2]
L.append(suma)
print L
plt.hist(L, 20, normed=1, facecolor='blue', alpha=0.75)
plt.xlabel('t(\mu s)')
plt.ylabel('Broj događaja')
plt.axis([0,10,0,300])
plt.grid(True)
plt.show()
EDIT: so I managed to deal with the ugly sums, but now my histograms don't work :(
Data is here: http://dropcanvas.com/kqjem
What's wrong? I get tons of errors and python crashes :\
The problem comes from having a discrete data set, it looks like you set the bins parameter to something that doesn't fit. Use the pylab.hist parameter histtype="stepfilled" to get them to touch without the lines. Here are a few examples:
import numpy as np
import pylab as plt
# Sample data
X1 = np.random.exponential(1.0,size=5000)
X2 = [int(z) for z in X1]
plt.subplot(221)
plt.hist(X1,bins=50)
plt.title('Continuous Data')
plt.subplot(222)
plt.hist(X2,bins=50)
plt.title('Discrete Data')
plt.subplot(223)
plt.hist(X2,histtype='stepfilled')
plt.title('Discrete Data Filled')
plt.show()
use numpy.histogram: http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
or matplotlib.pyplot.hist: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist
for example:
plt.hist(data, bins=20)