Matplotlib graph with typical linestyle not showing - python

I want a very simple plot:
import matplotlib.pyplot as plt
import numpy as np
for t in np.linspace(0,2*np.pi,100):
plt.plot(np.cos(t), np.sin(t), color='blue', linestyle='-', linewidth=7)
plt.show()
But nothing is appearing. I just get an empty plot. Where is my error?

Just plot the whole arrays:
import matplotlib.pyplot as plt
import numpy as np
t = np.linspace(0,2*np.pi,100):
plt.plot(np.cos(t), np.sin(t), color='blue', linestyle='-',linewidth=7)
plt.show()

Each call to plt.plot within the for loop is plotting a separate 'line' that consists on only a single point.
if you want the code to work you should plot points instead of lines.
for t in np.linspace(0,2*np.pi,100): plt.plot(np.cos(t), np.sin(t), 'k.')

Related

Why can't I see labels using pyplot [duplicate]

When I execute the following code, it doesn't produce a plot with a label.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
plt.plot(x, x*1.5, label='Normal')
Numpy version is '1.6.2'
Matplotlib version is '1.3.x'
Any ideas as to why this is happening?
You forgot to display the legend:
...
plt.legend(loc='best')
plt.show()

Showing end point of the 3d line: Python 3D plot

I made a 3D plot using the following code in python. Here three arrays x, y and z are used for the plot. I want to show the last point of the arrays (or the end point of the 3D line) in the plot. I used the approach I would use in 2d plotting, i.e., I asked for plotting only the last points of each array using this command ax.plot(x[-1],y[-1],z[-1],'o'). But it doesn't work.
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
x=np.linspace(0,2*np.pi)
y=np.sin(x)
z=np.cos(x)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, lw=1)
ax.plot(x[-1],y[-1],z[-1],'o') # This line doesn't work
plt.show()
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
x=np.linspace(0,2*np.pi)
y=np.sin(x)
z=np.cos(x)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, lw=1)
ax.scatter(x[-1],y[-1],z[-1],'-') # This should do the job
plt.show()
Add Color and Label
ax.scatter(x[-1],y[-1],z[-1],'-',c="yellow",label="End Point")
plt.legend()
plt.show()
Additional explanation on why you were having an error:
You were telling python to draw you a ax.plot for 1 point. Which is impossible, because you cant draw a line using 1 point only. Therefore, you tell it to draw a scatter.

Two different plots from same loop in matplotlib?

I would specifically like to create two different plots using one single loop. One plot should have four straight lines from x-y, and another plot should have four angled lines from x-y2. My code only shows everything in a single plot. I don't quite understand how plt works, how can I create two distinct plt objects?
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']
for i in range(len(x)):
plt.plot(x[i],y2[i],colours[i])
plt2.plot(x[i],y[i],colours[i])
plt.show()
plt2.show()
Is that what you want to do?
import matplotlib.pyplot as plt
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
for i in range(len(x)):
ax1.plot(x[i],y2[i],colours[i])
ax2.plot(x[i],y[i],colours[i])
fig1.show()
fig2.show()

Plot a sphere that looks like a sphere

So I am sampling from a 3D sphere and want to display it and despite the plt.axis('equal') command it still looks elliptic rather than spheric. Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def sphere_sampler(dimension=2,sample_size=1):
phi_1=np.random.uniform(low=0,high=np.pi,size=sample_size)
phi_2=np.random.uniform(low=0,high=2*np.pi,size=sample_size)
sample=np.empty((sample_size,dimension))
sample[:,0]=np.cos(phi_1)
sample[:,1]=np.sin(phi_1)*np.cos(phi_2)
sample[:,2]=np.sin(phi_1)*np.sin(phi_2)
return sample
pre_sample=sphere_sampler(3,1000)
sample=pre_sample.reshape(pre_sample.shape[0],3)
fig=plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(sample[:,0],sample[:,1],sample[:,2])
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(-1,1)
plt.axis('equal')
plt.show()
Which part of it I am doing wrong? It looks like that something is wrong with display. How can I make the show() method to not to change the scale?
instead of plt.axis('equal'), use:
ax.set_aspect("equal")

how to make rug plot in matplotlib

Im making a density plot with matplotlib and I would also like to get rug plot under it. good example to make density plot is here How to create a density plot in matplotlib?
but I couldn't find any good example for rug plot. in R it can be done easly by rug(data).
You can plot markers at each datapoint.
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
sample = np.hstack((np.random.randn(30), np.random.randn(20)+5))
density = stats.kde.gaussian_kde(sample)
fig, ax = plt.subplots(figsize=(8,4))
x = np.arange(-6,12,0.1)
ax.plot(x, density(x))
ax.plot(sample, [0.01]*len(sample), '|', color='k')
You can find an example here!
ax = fig.add_subplot(111)
ax.plot(x1, np.zeros(x1.shape), 'b+', ms=20) # rug plot
x_eval = np.linspace(-10, 10, num=200)
ax.plot(x_eval, kde1(x_eval), 'k-', label="Scott's Rule")
ax.plot(x_eval, kde1(x_eval), 'r-', label="Silverman's Rule")
Seems to be the core of it!
You can also use Seaborn.distplot, which wraps histogram, KDE and rugs altogether. Figures made by Seaborn are also prettier by default.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sample = np.hstack((np.random.randn(30), np.random.randn(20)+5))
fig, ax = plt.subplots(figsize=(8,4))
sns.distplot(sample, rug=True, hist=False, rug_kws={"color": "g"},
kde_kws={"color": "k", "lw": 3})
plt.show()
Here's the answer for people just looking for a rugplot to use on a matplotlib axis: you can use a seaborn function.
import seaborn as sns
sns.rugplot(xdata, height=0.025, axis=ax, color='k')
This looks much nicer than a pure-matplotlib kludge because the rug is aligned to (flush with) the x-axis.

Categories