Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know that there are several ways to graph using APIs in Python. But let's say I have the following code:
from random import randint
i = 1
while i < 1000:
num = randint(0,9)
print num
// Add num to a graph that is Random Number versus Time
// Show graph
What should I do such that I can generate a graph that has Random numbers (from 0->9) versus Time.
Following your code (changing 1000 to 100 for a clearer plot), you can do:
import matplotlib.pyplot as plt
import numpy as np
from random import randint
i = 1
numbers = [0]*100
while i < 100:
numbers[i] = randint(0,9)
i += 1
plt.plot(np.arange(1,101),numbers, 'o')
plt.xlabel('Time')
plt.show()
This will give you:
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 hours ago.
Improve this question
When a variable is normalized, its value will change and its sum will be 1. However, the value obtained when the variable is later substituted does not change. Can anyone help to solve this problem? Let's put the specific example below. Normalize fy, y does not change, no matter yF will change, but dy has not changed. I hope you can solve this problem.
fy_ = 0
for i in range(len(fy)):
fy_ += fy[i]
fy = fy / fy_
print(sum(fy))
yF = 0
for i in range(len(y) - 1):
yF += (y[i + 1] -y[i]) * (y[i] * fy[i])
print(yF)
dy = y * fy / yF
print(dy)
I have used different y, fy has tried many times, and every time dy basically does not change, which makes me wonder why this phenomenon occurs
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to understand how numpy.interp function works. This function has a couple of parameters which are vague to me, like: xp, fp, left and right.
I saw an example of this function as below:
X is a numpy ndarray and y is one-dimensional array:
X = np.interp(X, (X.min(), X.max()), (0, 10))
y = np.interp(y, (y.min(), y.max()), (30000, 100000))
Thanks for helping me out!
x are the points which are not in xp but you want their y values i.e. points where you want to perform interpolation
xp and yp are the main inputs based on which 1D interpolation works (these are the discrete data points through which you want to interpolate)
left and right handle the edge case scenarios when x is out of the closed interval range of [xp_minimum, xp_maximum] i.e. the output values to generate incase of extrapolation
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I try to plot a sinus function. But it throws an error" x and y must have same first dimension, but have shapes (1,) and (51,)" I can't understand they have same dimension. Why do I get this error? Thanks in advance.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from numpy import *
# spatial domain
xmin = 0
xmax = 1
n = 50 # num of grid points
# x grid of n points
x = np.linspace(xmin, xmax, n+1);
k=2
def f1(x):
return np.sin(2*pi*k*x)
plt.plot(f1,x)
Change the bottom of your program to be semantically correct:
plt.plot(f1(x),x)
plt.show()
f1 is a function object; f1(x) is a vector of return values.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My data has the following structure and it has 50 000 simples in wich duration is ranged between 2 secondes and 10 day
Id duration y
1 23 0
2 64 1
3 15 1
I would like to draw a plot that can visualize the vriation of duration and the probability to get outcome positive.
I need to work with python this problem
what you want is a histgram of duration and a probability distribution of y.
import math
import matplotlib.pyplot as plt
duration=[23,64,15,18,1,63,21,27]
y=[0,1,1,0,0,1,0,1]
BUCKET_SIZE=10
numToBucket=lambda x: round(math.ceil(x/BUCKET_SIZE))
yCount=[0]*numToBucket(max(duration))
hist=[0]*numToBucket(max(duration))
for i in range(len(duration)):
index=numToBucket(duration[i])-1
hist[index]+=1
yCount[index]+=y[i]
probDist=[0 if (yCount[i]==0) else yCount[i]/hist[i] for i in range(len(hist))]
axis=[i*BUCKET_SIZE for i in range(len(hist))]
print(hist)
print(yCount)
print(probDist)
plt.bar(axis, hist, align='center', alpha=0.5)
plt.title('Histogram')
plt.show()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a loop
for i in range(0,1000,100):
and inside it I compute a list which holds 10 values but the loop goes from 0 to 1000.I want to relate these 10 values with the 1000 values; namely, create a list (or array) which will hold these values (the 10 to 1000 values).
UPDATED------------------------------------------
I want to make a plot which will have in the horizontal axis values from 0 to 1000 and in the vertical axis the 10 values of the list that i computed.
Your question is very unclear.
From your comment it seems like you're asking about matplotlib? Do you want something like this?
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1000, 100)
# As a placeholder for your calculation...
y = np.cos(x / 100.0)
plt.plot(x, y, marker='o', mfc='red')
plt.show()
Do you want to access the list elements while retaining the range from 0 to 1000 in steps of 100? If so, this should be a way.
mylst=[12,5,6,34,6,11,78,1,1,88]
for i in range(0,1000,100):
print mylst[i/100]
Not sure... a more detailed question could help.