I am a beginner at this, and I am needing to make a triangle and have it graphed on my VM in Linux. Using Python, the first step would be to generate just three random points for the triangle. How would be the best way to accomplish this? Any help, tips, or advice would be appreciated!
Using matplotlib as indicated in the tags:
import matplotlib.pyplot as plt
data = [[6, 2, 3], [1, 2, 3]]
plt.plot(data[0] + [data[0][0]], data[1] + [data[1][0]], marker='o', color='blue')
plt.show()
To generate random data: (look up the random module to choose the appropriate generator)
import random
data = [[random.randrange(10) for _ in range(3)], [random.randrange(10) for _ in range(3)]]
With pylab commands (Numpy and Matplotlib) you can do:
>>> a = rand(3, 2); a = vstack((a, a[0])); plot(a[:, 0], a[:, 1]); show()
You need something like python -i -c 'from pylab import *' to get the Numpy and Matplotlib functions in the namespace.
Related
More specifically how do I change it to work like this graph ? I've tried using plt.yscale() but to no avail as it only allows certain set values and I didn't get very far with using plt.axis. This code is a simple attempt at a linear regression with the values shown below, my coefficients (for a function A+Bx) were A=38.99 and B=2.055
X=np.array([2,4,6,8,10])
Y=np.array([42.0,48.4,51.3,56.3,58.6])
A, B=P.polyfit(X,Y,1)
plt.plot(X,Y,'o')
plt.plot(X,A+B*X)
plt.yscale('linear')
plt.show()
And my graph comes out looking like this:graph2 Which isn't wrong but I got curious on how to make it look like the one above and just couldn't figure it out.
I'm using Matplotlib's Object-Oriented API.
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline # only needed if running from a Jupiter Notebook
import numpy as np
X = np.array([2,4,6,8,10])
Y = np.array([42.0,48.4,51.3,56.3,58.6])
A, B = np.polyfit(X,Y,1)
fig, ax = plt.subplots()
ax.plot(X, Y, 'o')
ax.plot(X, B+A*X)
ax.xaxis.set_major_locator(mpl.ticker.FixedLocator([0, 2, 4, 6, 8, 10]))
ax.yaxis.set_major_locator(mpl.ticker.FixedLocator([40, 50, 60]))
ax.set(xlim=[0, 11], ylim=[40, 60], xlabel=r'Mass (kg) $->$', ylabel=r'Length (cm) $->$');
Hii experts i have written a simple python script for accessing the list of values and does some calculation in for loop,while plotting(x,y) it doesn't give the plot.My programme is given below.i hope some expert will help me rectifying the problem.Thanks in advance.
import math
import numpy as np
import matplotlib.pyplot as plt
a=1978780
b=[4,40,90,100,600,785,900]
for i in range(len(b)):
zx=math.exp(b[i]/a)*5
# print(b[i],zx)
plt.scatter(b[i],zx)
plt.show()
This will solve the issue. (But not recommended try the other solution, fast and efficient)
Or this
a=1978780
b=np.array([4,40,90,100,600,785,900])
zx=np.exp(b/a)*5
plt.plot(b,zx)
plt.show()
or this
a=1978780
b=np.array([4,40,90,100,600,785,900])
for i in range(len(b)):
zx=math.exp(b[i]/a)*5
# print(b[i],zx)
plt.scatter(b[i],zx)
plt.ylim(4.999,5.005)
plt.show()
Can't you apply the vectorized operation on the numpy array and plot it?
import matplotlib.pyplot as plt
import numpy as np
a=1978780
b=np.array([4,40,90,100,600,785,900])
zx=np.exp(b/a)*5
plt.plot(b,zx)
plt.show()
You want to use numpy's ability to work on arrays. That way, no need to iterate over lists anymore and you can write your function in a single line.
import matplotlib.pyplot as plt
import numpy as np
a = 1978780
b = np.array([4, 40, 90, 100, 600, 785, 900])
zx = np.exp(b/a)*5
fig, ax = plt.subplots()
ax.plot(b, zx)
fig.show()
For a while, I've been using both seaborn and plotly for visualization, depending on my needs at the moment. Lately, I've been trying to move completely to plotly, but there are things that I still can't find out how to make it work.
For example, I used to use seaborn to check the distribution of some data, to see how well it fitted to the gaussian distribution. This can be easily done with the following snippet:
import seaborn as sns
from scipy.stats import norm
sns.distplot(data, fit=norm)
I've been trying to achieve some similar quick gaussian check with plotly express (px.histogram to be more specific), but I can't get it done. Could you please help me with this matter?
EDIT
An example for "data" would be:
import numpy as np
np.random.seed(123)
data = np.random.noncentral_chisquare(3, 20, 1000)
The output should show data histogram with its KDE, plus a gaussian equivalent KDE. This is helpful when testing transformations results (log, box-cox...)
I think you can be interested in reading this. Apparently at the moment the easiest way it's using plotly.figure_factory.create_dist_plot but from the link above it looks like it's going to be discontinued.
import numpy as np
import plotly.figure_factory as ff
np.random.seed(123)
data = np.random.noncentral_chisquare(3, 20, 1000)
m = data.mean()
s = data.std()
gaussian_data = np.random.normal(m, s, 10000)
fig = ff.create_distplot(
[data, gaussian_data],
group_labels=["plot", "gaussian"],
curve_type="kde")
fig.data = [fig.data[0], fig.data[2], fig.data[3]]
fig.update_layout(showlegend=False)
fig.show()
And if instead of fig.data = ... you use
lst = list(fig.data)
lst.pop(1)
fig.data = tuple(lst)
you'll get
I'm very new to python just started using it from a day or two..
I'm using Anaconda python notebook.
so I'm trying to plot, but in the output there is only grid and nothing no lines or anything,
my program is as follows
from __future__ import print_function
from decimal import *
import numpy as np
from sympy import *
import pylab
k = Symbol('k')
A = Symbol('A')
E = Symbol('E')
d = Symbol('d')
C = Symbol('C')
Y = Symbol('Y')
Y = []
for A in np.arange(-1.11, 1.11, 0.002):
s = sin(A)
c = cos(A)
C = (s/A) + c
Y.append(C)
pylab.plot(C, A)
grid()
xlabel('$x$')
ylabel('$y$')
title('graph')
The code doesn't show any errors, but will you please help me as to what am I doing wrong here ...
You are mixing different plotting functions from pylab, sympy and you are not giving an X axis:
import numpy as np
from matplotlib import pyplot
Y=[]
X = np.arange(-1.11, 1.11, 0.002)
for A in X:
s = np.sin(A)
c = np.cos(A)
C = (s/A)+c
Y.append(C)
line, = pyplot.plot(X,Y, "-b")
pyplot.grid(True)
pyplot.show()
Gives me:
What about showing the graph with
pylab.show()
If I do this, I have a figure with both grid and graph:
import pylab
pylab.plot([1, 3, 4], [1, 2, 3])
pylab.grid()
pylab.show()
But if I do this, I have first a figure with only the graph, and then with only the grid:
import pylab
pylab.plot([1, 3, 4], [1, 2, 3])
pylab.show() # here I get only the graph
pylab.grid()
pylab.show() # here I get only the grid
Note: calling grid(), title(), xlabel and ylabel as you do shall not work; each time it shall be prepended by pylab.. Is that really your code?
I want to plot the data points that are in a 1-D array just along the horizontal axis [edit: at a given y-value], like in this plot:
How can I do this with pylab?
Staven already edited his post to include how to plot the values along y-value 1, but he was using Python lists.
A variant that should be faster (although I did not measure it) only uses numpy arrays:
import numpy as np
import matplotlib.pyplot as pp
val = 0. # this is the value where you want the data to appear on the y-axis.
ar = np.arange(10) # just as an example array
pp.plot(ar, np.zeros_like(ar) + val, 'x')
pp.show()
As a nice-to-use function that offers all usual matplotlib refinements via kwargs this would be:
def plot_at_y(arr, val, **kwargs):
pp.plot(arr, np.zeros_like(arr) + val, 'x', **kwargs)
pp.show()
This will plot the array "ar":
import matplotlib.pyplot as pp
ar = [1, 2, 3, 8, 4, 5]
pp.plot(ar)
pp.show()
If you are using ipython, you can start it with the "-pylab" option and it will import numpy and matplotlib automatically on startup, so you just need to write:
ar = [1, 2, 3, 8, 4, 5]
plot(ar)
To do a scatter plot with the y coordinate set to 1:
plot(ar, len(ar) * [1], "x")
X = np.arange(10)
plt.scatter( X, [0] * X.shape[0])
Click on the link to check the plot