I plotted the picture using the code below.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(6)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(5):
ax.set_xlim(0, 3)
ax.set_ylim(0, 10)
ax.plot(x, i * x)
plt.show()
This is the result picture.
enter image description here
This is the picture I want to see.
I want to get the results out of the box area.
How can you draw such a plot?
enter image description here
This should work -
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(6)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(5):
ax.set_xlim(0, 3)
ax.set_ylim(0, 10)
ax.plot(x, i * x)
if 3*i >10:
ytx = 10.5
xtx = 10.0/i
else:
ytx = 3*i
xtx = 3.05
tx = plt.text(xtx, ytx, str(i), fontsize=18, color='black')
plt.show()
This generates -
Related
Let's see a contrived example of 2 matplotlib.pyplot functions returning a plot, which then may be accessed through plt.gcf() or plt.gca() methods:
x = np.linspace(-5, 5, 11)
y = x
y_2 = x * x
plt.plot(x,y)
fig_1 = plt.gcf()
ax_1 = plt.gca()
#plt.close()
plt.plot(x,y_2)
fig_2 = plt.gcf()
ax_2 = plt.gca()
#plt.close()
How would I nicely draw fig_1 and fig_2 objects alongside through e.g. plt.subplots(1,2) (given x, y, y_2 are not accessable)
Use the add_subplot method of the Figure object to draw the two plots alongside.
Try this:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 11)
y = x
y_2 = x * x
fig, axs = plt.subplots(1, 2) # create a figure with subplots
axs[0].plot(x, y) # first figure
axs[1].plot(x, y_2) # second figure
plt.show()
if no given given x, y, y_2:
fig, axs = plt.subplots(1, 2)
axs[0].plot(x, y)
axs[1].plot(x, y_2)
plt.show()
I am trying to use Matplotlib to generate plot axes within a function called within a for-loop and use these axes to create a final multi-panel figure. However, despite the axes showing the proper lines when they are being created within my function, the multi-panel figure ends up with empty axes. What am I doing wrong? My code is below.
import numpy as np
import matplotlib.pyplot as plt
def create_axis(alpha, beta):
fig, ax = plt.subplots(figsize=(5, 4))
x = np.arange(10, dtype=float)
y = alpha + x * beta
ax.plot(x, y)
return ax
def create_plot():
alpha = 3.
axes_pool = []
for i in range(4):
axes_pool.append(create_axis(alpha, i))
fig, axes = plt.subplots(1, 4)
for i in range(len(axes)):
axes[i] = axes_pool[i]
plt.show()
if __name__ == '__main__':
create_plot()
You are passing the axes to the subfunction to allow the plt on the already created subplots.
Right approach is sending the axes as one of the parameters for create_axis function,which will plot the required data in the supplied axis.
import numpy as np
import matplotlib.pyplot as plt
def create_axis(ax,alpha, beta):
x = np.arange(10, dtype=float)
y = alpha + x * beta
ax.plot(x, y)
def create_plot():
alpha = 3.
fig, axes = plt.subplots(1, 4,figsize=(5,4)))
for i,ax in enumerate(axes):
create_axis(ax,alpha,i)
if __name__ == '__main__':
create_plot()
plt.show()
output:
I'm trying to plot 23 graphs in a 6x4 grid, with one figure taking up twice the width of the other figures. I'm using gridspec and my current code is:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.arange(0, 7, 0.01)
fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=4)
for n in range(22):
ax = fig.add_subplot(gs[n])
ax.plot(x, np.sin(0.2*n*x))
corrax = fig.add_subplot(gs[22])
fig.tight_layout()
plt.show()
This produces the following:
I want to increase the width of the rightmost plot in the bottom row so it takes up the remaining space in that row. Is there a way to accomplish this?
You can use slices to select several positions from the gridspec, e.g. gs[22:24].
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.arange(0, 7, 0.01)
fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=4)
for n in range(22):
ax = fig.add_subplot(gs[n])
ax.plot(x, np.sin(0.2*n*x))
corrax = fig.add_subplot(gs[22:24])
corrax.plot(x,np.sin(0.2*22*x), color="crimson", lw=3)
fig.tight_layout()
plt.show()
You can also slice the gridspec two-dimensionally. E.g. to create a 3x3 grid and make the plot in the lower right corner span two columns and two rows, you could slice like gs[1:,1:].
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.arange(0, 7, 0.01)
fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=3, ncols=3)
for n in range(3):
ax = fig.add_subplot(gs[0,n])
ax.plot(x, np.sin(0.2*n*x))
if n !=0:
ax = fig.add_subplot(gs[n,0])
ax.plot(x, np.sin(0.2*n*x))
corrax = fig.add_subplot(gs[1:,1:])
corrax.plot(x,np.sin(0.2*22*x), color="crimson", lw=3)
fig.tight_layout()
plt.show()
#corrax = fig.add_subplot(gs[5,2:])
corrax = fig.add_subplot(6,4,(23,24))
both shold work.
see examples
I am trying to do a matplolib figure with some padding between the axis and the actual plot.
Here is my example code :
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
plt.plot(x, y, 'r')
plt.grid(True)
plt.show()
And here is what I am trying to get :
In your case, it's easiest to use ax.margins(some_percentage) or equivalently plt.margins(some_percentage).
For example:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
ax.plot(x, y, 'r')
ax.grid(True)
ax.margins(0.05) # 5% padding in all directions
plt.show()
you can set the limits of the plot with xlim and ylim.
See here
I have code for "live" plotting with Matplotlib in Python, but it closes once it's done. I would like the plot to remain open.
Code below
import time
import matplotlib.pyplot as plt
plt.ion()
plt.show()
for i in range(10):
time.sleep(1)
x = i ** 2
plt.scatter(i, x)
plt.draw()
Maybe you want something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def make_data():
for i in range(100):
yield i, i*2
fig, ax = plt.subplots()
color = plt.cm.cubehelix(np.linspace(0.1,0.9,100))
plot, = ax.plot([], [],'o')
xdata, ydata = [], []
ax.set_ylim(0, 1)
ax.set_xlim(0, 1)
def run(data):
x,y = data
xdata.append(x)
ydata.append(y)
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
if y > ymax:
ax.set_xlim(xmin, 1+xmax)
ax.set_ylim(ymin, 1+ymax)
ax.figure.canvas.draw()
plot.set_color(color[x])
plot.set_data(xdata,ydata)
return plot,
ani = animation.FuncAnimation(fig,run,make_data,blit=True,interval=10,repeat=False)
plt.show()
Maybe scatter would be better since it might allow for different colors of each circle.