Defining multiple plots to be animated with a for loop in matplotlib

In the solution below I showcase a bigger example (with also bar plot) that may help people understand better what should be done for other cases. After the code I explain some details and answer the bonus question. import matplotlib matplotlib.use(‘Qt5Agg’) #use Qt5 as backend, comment this line for default backend from matplotlib import pyplot … Read more

Multiple font sizes in plot title

Try: import matplotlib.pyplot as plt plt.rc(‘text’, usetex=True) plt.title(r’\fontsize{30pt}{3em}\selectfont{}{Mean WRFv3.5 LHF\r}{\fontsize{18pt}{3em}\selectfont{}(September 16 – October 30, 2012)}’) plt.show() That \r might want to be a \n on your system. I used Joel’s answer to address your question.

Plot all pandas dataframe columns separately

Pandas subplots=True will arange the axes in a single column. import numpy as np import matplotlib.pyplot as plt import pandas as pd df = pd.DataFrame(np.random.rand(7,20)) df.plot(subplots=True) plt.tight_layout() plt.show() Here, tight_layout isn’t applied, because the figure is too small to arange the axes nicely. One can use a bigger figure (figsize=(…)) though. In order to have … Read more

How to show minor tick labels on log-scale with Matplotlib

You can use plt.tick_params(axis=”y”, which=”minor”) to set the minor ticks on and format them with the matplotlib.ticker FormatStrFormatter. For example, import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter x = np.linspace(0,4,1000) y = np.exp(x) plt.plot(x, y) ax = plt.gca() ax.set_yscale(‘log’) plt.tick_params(axis=”y”, which=”minor”) ax.yaxis.set_minor_formatter(FormatStrFormatter(“%.1f”)) plt.show()

How to show minor tick labels on log-scale with Matplotlib

You can use plt.tick_params(axis=”y”, which=”minor”) to set the minor ticks on and format them with the matplotlib.ticker FormatStrFormatter. For example, import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter x = np.linspace(0,4,1000) y = np.exp(x) plt.plot(x, y) ax = plt.gca() ax.set_yscale(‘log’) plt.tick_params(axis=”y”, which=”minor”) ax.yaxis.set_minor_formatter(FormatStrFormatter(“%.1f”)) plt.show()

How can I make a video from array of images in matplotlib?

For a future myself, here is what I ended up with: def generate_video(img): for i in xrange(len(img)): plt.imshow(img[i], cmap=cm.Greys_r) plt.savefig(folder + “/file%02d.png” % i) os.chdir(“your_folder”) subprocess.call([ ‘ffmpeg’, ‘-framerate’, ‘8’, ‘-i’, ‘file%02d.png’, ‘-r’, ’30’, ‘-pix_fmt’, ‘yuv420p’, ‘video_name.mp4’ ]) for file_name in glob.glob(“*.png”): os.remove(file_name)

Plot pandas dates in matplotlib

If you use a list containing the column name(s) instead of a string, data.set_index will work The following should show the dates on x-axis: #! /usr/bin/env python import pandas as pd import matplotlib.pyplot as plt data = pd.read_fwf(‘myfile.log’,header=None,names=[‘time’,’amount’],widths=[27,5]) data.time = pd.to_datetime(data[‘time’], format=”%Y-%m-%d %H:%M:%S.%f”) data.set_index([‘time’],inplace=True) data.plot() #OR plt.plot(data.index, data.amount)