Why the ax.plot can not show in terminal in Windows by using matplotlib - matplotlib

You can see I am using ax.plot but nothing happened.

Just call plt.show() when you've run all of your graph creation code:
import numpy as np
import matplotlib.pyplot as plt
x= np.arange(-10, 10, 0.1)
fig, ax = plt.subplots()
ax.plot(x, np.cos(x))
plt.show()

Related

how to plot Figure instances from other scripts

Is it possible to save and load the plot with all its attributes? E.g. pickle the Figure instance and then opening it in another script and redrawing as it was in the original script.
Script1
import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.figure()
pd.to_pickle(fig,'fig.pkl')
Script2
import matplotlib.pyplot as plt
import pandas as pd
fig = pd.read_pickle(fig,'fig.pkl')
# Now plot it so that it looks like in script1
You can use pickle.dump to save:
import matplotlib.pyplot as plt
import pickle
fig, ax = plt.subplots()
pickle.dump(fig, open('fig.pkl', 'wb'))
And pickle.load to recover:
import matplotlib.pyplot as plt
import pickle
fig = pickle.load(open('fig.pkl', 'rb'))
plt.show()
Re: comment about storing figs in a dict
This works on my end -- dump the dict of figure handles:
import matplotlib.pyplot as plt
import pickle
fig1, ax1 = plt.subplots()
ax1.plot([0, 1], [0, 1])
fig2, ax2 = plt.subplots()
ax2.plot([1, 0], [0, 1])
figs = {'fig1': fig1, 'fig2': fig2}
pickle.dump(figs, open('figs.pickle', 'wb'))
Then load the dict and access the desired dict key:
import matplotlib.pyplot as plt
import pickle
figs = pickle.load(open('figs.pickle', 'rb'))
figs['fig1'] # or figs['fig2']

How to hide axes in multiple plot

What is wrong for this code for hiding right and top axes, please?
import matplotlib.pyplot as plt
fig, ax = plt.subplots(sharex=True, sharey=True, figsize=(10,3))
fig1 = plt.subplot(121)
fig2 = plt.subplot(122)
# Set width of axes
for figures in [fig1, fig2]:
# Removing axis
for side in ['right','top']:
ax.spines[side].set_visible(False)
plt.show()
This works for non-multiple plot:
for side in ['right','top']:
ax.spines[side].set_visible(False)
EDITED CODE:
import matplotlib.pyplot as plt
import seaborn as sns
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(10,3))
fig1 = plt.subplot(121)
ax1.set_xlabel(r'$k$')
ax1.set_ylabel(r'$\omega$', rotation='horizontal')
fig2 = plt.subplot(122)
sns.despine()
plt.show()

Record interactive plot

The following code works fine to save an animation to file:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, bitrate=1800)
fig, ax = plt.subplots()
ims = []
x = np.linspace(0, np.pi,200)
for theta in np.linspace(0, np.pi, 50):
plot = ax.plot(x, np.sin(x + theta))
ims.append(plot)
im_ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True)
im_ani.save('im.mp4', writer=writer)
Now, I would like to view the animation interactively as the plots are generated, while still saving it to file. I therefore tried the following code:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, bitrate=1800)
plt.ion()
fig, ax = plt.subplots()
ims = []
x = np.linspace(0, np.pi, 200)
for theta in np.linspace(0, np.pi, 50):
ax.clear()
plot = ax.plot(x, np.sin(x + theta))
ims.append(plot)
plt.draw()
plt.pause(0.01)
im_ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True)
im_ani.save('im.mp4', writer=writer)
which lets me view the animation interactively, but the resulting video file contains only blank frames.
Is is possible to view an animation interactively and save it to file at the same time? What is the issue with my code?

Labeling along the axes, matplotlib

How do I place the labeling of the axes inside of the graph?
Right now i have this:
I want the numbering inside of the graph, along the axes.
Thanks,
Cro.
Looking at the matplotlib spine placement demo you'd find that you can shift the spines using ax.spines['left'].set_position('zero').
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
x = np.linspace(-3,3)
y = x**2
fig, ax = plt.subplots()
ax.set_ylim(-4,10)
ax.set_xlim(-10,10)
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
plt.show()

Set zlim in matplotlib scatter3d

I have three lists xs, ys, zs of data points in Python and I am trying to create a 3d plot with matplotlib using the scatter3d method.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.xlim(290)
plt.ylim(301)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.scatter(xs, ys, zs)
plt.savefig('dateiname.png')
plt.close()
The plt.xlim() and plt.ylim() work fine, but I don't find a function to set the borders in z-direction. How can I do so?
Simply use the set_zlim function of the axes object (like you already did with set_zlabel, which also isn't available as plt.zlabel):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
xs = np.random.random(10)
ys = np.random.random(10)
zs = np.random.random(10)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.scatter(xs, ys, zs)
ax.set_zlim(-10,10)