How to do dynamic matplotlib plotting with a fixed pandas dataframe? - python

I have a dataframe called benchmark_returns and strategy_returns. Both have the same timespan. I want to find a way to plot the datapoints in a nice animation style so that it shows all the points loading in gradually. I am aware that there is a matplotlib.animation.FuncAnimation(), however this typically is only used for a real-time updating of csv files etc but in my case I know all the data I want to use.
I have also tried using the crude plt.pause(0.01) method, however this drastically slows down as the number of points get plotted.
Here is my code so far
x = benchmark_returns.index
y = benchmark_returns['Crypto 30']
y2 = benchmark_returns['Dow Jones 30']
y3 = benchmark_returns['NASDAQ']
y4 = benchmark_returns['S&P 500']
fig, ax = plt.subplots()
line, = ax.plot(x, y, color='k')
line2, = ax.plot(x, y2, color = 'b')
line3, = ax.plot(x, y3, color = 'r')
line4, = ax.plot(x, y4, color = 'g')
def update(num, x, y, y2, y3, y4, line):
line.set_data(x[:num], y[:num])
line2.set_data(x[:num], y2[:num])
line3.set_data(x[:num], y3[:num])
line4.set_data(x[:num], y4[:num])
return line, line2, line3, line4,
ani = animation.FuncAnimation(fig, update, fargs=[x, y, y2, y3, y4, line],
interval = 1, blit = True)
plt.show()

You could try matplotlib.animation.ArtistAnimation. It operates similar to FuncAnimation in that you can specify the frame interval, looping behavior, etc, but all the plotting is done at once, before the animation step. Here is an example
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.animation import ArtistAnimation
n = 150
x = np.linspace(0, np.pi*4, n)
df = pd.DataFrame({'cos(x)' : np.cos(x),
'sin(x)' : np.sin(x),
'tan(x)' : np.tan(x),
'sin(cos(x))' : np.sin(np.cos(x))})
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(10,10))
lines = []
artists = [[]]
for ax, col in zip(axs.flatten(), df.columns.values):
lines.append(ax.plot(df[col])[0])
artists.append(lines.copy())
anim = ArtistAnimation(fig, artists, interval=500, repeat_delay=1000)
The drawback here is that each artist is either drawn or not, i.e. you can't draw only part of a Line2D object without doing clipping. If this is not compatible with your use case then you can try using FuncAnimation with blit=True and chunking the data to be plotted each time as well as using set_data() instead of clearing and redrawing on every iteration. An example of this using the same data from above:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.animation import FuncAnimation
n = 500
nf = 100
x = np.linspace(0, np.pi*4, n)
df = pd.DataFrame({'cos(x)' : np.cos(x),
'sin(x)' : np.sin(x),
'tan(x)' : np.tan(x),
'sin(cos(x))' : np.sin(np.cos(x))})
fig, axs = plt.subplots(2, 2, figsize=(5,5), dpi=50)
lines = []
for ax, col in zip(axs.flatten(), df.columns):
lines.append(ax.plot([], lw=0.5)[0])
ax.set_xlim(x[0] - x[-1]*0.05, x[-1]*1.05)
ax.set_ylim([min(df[col].values)*1.05, max(df[col].values)*1.05])
ax.tick_params(labelbottom=False, bottom=False, left=False, labelleft=False)
plt.subplots_adjust(hspace=0, wspace=0, left=0.02, right=0.98, bottom=0.02, top=0.98)
plt.margins(1, 1)
c = int(n / nf)
def animate(i):
if (i != nf - 1):
for line, col in zip(lines, df.columns):
line.set_data(x[:(i+1)*c], df[col].values[:(i+1)*c])
else:
for line, col in zip(lines, df.columns):
line.set_data(x, df[col].values)
return lines
anim = FuncAnimation(fig, animate, interval=2000/nf, frames=nf, blit=True)
Edit
In response to the comments, here is the implementation of a chunking scheme using the updated code in the question:
x = benchmark_returns.index
y = benchmark_returns['Crypto 30']
y2 = benchmark_returns['Dow Jones 30']
y3 = benchmark_returns['NASDAQ']
y4 = benchmark_returns['S&P 500']
line, = ax.plot(x, y, color='k')
line2, = ax.plot(x, y2, color = 'b')
line3, = ax.plot(x, y3, color = 'r')
line4, = ax.plot(x, y4, color = 'g')
n = len(x) # Total number of rows
c = 50 # Chunk size
def update(num):
end = num * c if num * c < n else n - 1
line.set_data(x[:end], y[:end])
line2.set_data(x[:end], y2[:end])
line3.set_data(x[:end], y3[:end])
line4.set_data(x[:end], y4[:end])
return line, line2, line3, line4,
ani = animation.FuncAnimation(fig, update, interval = c, blit = True)
plt.show()
or, more succinctly
cols = benchmark_returns.columns.values
# or, for only a subset of the columns
# cols = ['Crypto 30', 'Dow Jones 30', 'NASDAQ', 'S&P 500']
colors = ['k', 'b', 'r', 'g']
lines = []
for c, col in zip(cols, colors):
lines.append(ax.plot(benchmark_returns.index, benchmark_returns[col].values, c=c)[0])
n = len(benchmark_returns.index)
c = 50 # Chunk size
def update(num):
end = num * c if num * c < n else n - 1
for line, col in zip(lines, cols):
line.set_data(benchmark_returns.index, benchmark_returns[col].values[:end])
return lines
anim = animation.FuncAnimation(fig, update, interval = c, blit=True)
plt.show()
and if you need it to stop updating after a certain time simply set the frames argument and repeat=False in FuncAnimation().

You can just update the data into the line element like so:
fig = plt.figure()
ax = fig.add_subplot(111)
liner, = ax.plot()
plt.ion()
plt.show()
for i in range(len(benchmark_returns.values)):
liner.set_ydata(benchmark_returns['Crypto 30'][:i])
liner.set_xdata(benchmark_returns.index[:i])
plt.pause(0.01)

Related

Unable to refresh plt.axhline() in matplotlib

I'm just trying to make a live graph using matplotlib.However I couldn't find a way to draw-remove-redraw axhline(). My aim is to show a horizontal line of newest value of Y axis values and of course remove the recent horizontal line.
`
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import time
from random import randrange
style.use("fivethirtyeight")
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
#ax1 = plt.subplot()
second = 1
xs = list()
ys = list()
ann_list = []
a = 0
ten = 10
def animate(i):
global second
global a, ten
random = randrange(ten)
ys.append(random)
xs.append(second)
second += 1
ax1.plot(xs, ys, linestyle='--', marker='o', color='b')
plt.axhline(y = ys[-1], linewidth=2, color='r', linestyle='-')
if len(xs) > 2:
plt.axhline(y = ys[-2], linewidth=2, color='r', linestyle='-').remove()
if len(ys) > 20 and len(xs) > 20:
ax1.lines.pop(0)
ys.pop(0)
xs.pop(0)
a += 1
ax1.set_xlim(a, (21 + a))
# ax1.set_ylim(0, 200)
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
`
expecting that to only show the newest y axis values with a horizontal line. However horizontal lines doesn't vanish away.
In your code, this:
plt.axhline(y = ys[-2], linewidth=2, color='r', linestyle='-').remove()
doesn't remove the previous axhline; it adds a new axhline at y=ys[-2] and then immediately removes it. So, it effectively does nothing.
You have to remove the same line you inserted with plt.axhline. Save the object returned by this function somewhere, and remove it when the next frame is animated.
Here's a solution with a bit of default mutable argument abuse.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import time
from random import randrange
style.use("fivethirtyeight")
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
second = 1
xs = list()
ys = list()
ann_list = []
a = 0
ten = 10
def animate(i, prev_axhline=[]):
global second
global a, ten
random = randrange(ten)
ys.append(random)
xs.append(second)
second += 1
ax1.plot(xs, ys, linestyle='--', marker='o', color='b')
if prev_axhline:
prev_axhline.pop().remove()
prev_axhline.append(plt.axhline(y = ys[-1], linewidth=2, color='r', linestyle='-'))
if len(ys) > 20 and len(xs) > 20:
ax1.lines.pop(0)
ys.pop(0)
xs.pop(0)
a += 1
ax1.set_xlim(a, (21 + a))
# ax1.set_ylim(0, 200)
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()

Matplotlib Animation : Graph can't appear

Recently I have managed to do the animation. But now I can't make the tangent line went as I want (not even displayed yet). The formula of the tangent line is y=(2/r)(sqrt(1-((r^2)/4))-1)x +r. The formula is obtained from 2 circles equation (C1 and C2). C1(blue) : x^2+y^2=r^2, and C2(green) : (x-1)^2+y^2=1. My goal is to obtain this kind of animation as and my current animation goes like this .
How should the code looks like when the animation looks like the reference one (the first one)? Any comments and answers would be very helpful for me as a beginner, I appreciate it, Thank you.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(1)
line, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
# theta goes from 0 to 2pi
theta = np.linspace(0, 2*np.pi, 100)
# the radius of the circle
r = np.sqrt(1)
r2 = np.sqrt(4)
# compute x1 and x2
x1 = 1+r*np.cos(theta)
y1 = r*np.sin(theta)
x2 = r2*np.cos(theta)
y2 = r2*np.sin(theta)
# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
def init():
line.set_data([], [])
return line,
def init2():
line.set_data([], [])
return line2,
def animate(i):
x2 = np.sqrt(i)*np.cos(theta)
y2 = np.sqrt(i)*np.sin(theta)
line.set_data(x2, y2)
return line,
def animate2(i):
x3 = np.linspace(0,r2**2,100)
y3 = ((2/r2)*(np.sqrt(1-(r2**2)/4)-1)*x3)+r2
line.set_data(x3, y3)
return line,
# create the figure
ax.plot(x1,y1)
ax.set_aspect(1)
plt.grid()
anim = animation.FuncAnimation(fig, animate, init_func=init,
interval=1000, blit=False,\
frames=np.hstack([range(0),
range(4)[::-1]]))
anim2 = animation.FuncAnimation(fig, animate2, init_func=init2,
interval=1000, blit=False)
plt.show()
f = r"D:/UNPAR/Semester 2/Pemrograman Komputer/Project/SK.gif"
writergif = animation.PillowWriter(fps=30)
anim.save(f, writer=writergif)
The animation functions need to be combined into one. We will combine them into an initialization function and an animation function.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(1)
line, = ax.plot([], [], lw=2, color='green')
line2, = ax.plot([], [], lw=2, color='red')
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
# theta goes from 0 to 2pi
theta = np.linspace(0, 2*np.pi, 100)
# the radius of the circle
r = np.sqrt(1)
r2 = np.sqrt(4)
# compute x1 and x2
x1 = 1+r*np.cos(theta)
y1 = r*np.sin(theta)
x2 = r2*np.cos(theta)
y2 = r2*np.sin(theta)
# create the figure
ax.plot(x1,y1)
ax.set_aspect(1)
plt.grid()
def init():
line.set_data([], [])
line2.set_data([], [])
return line,line2
def animate(i):
x2 = np.sqrt(i)*np.cos(theta)
y2 = np.sqrt(i)*np.sin(theta)
print(max(y2))
line.set_data(x2, y2)
x3 = [0, 4-(0.1*i)]
y3 = [max(y2), 0]
line2.set_data(x3, y3)
return line,line2
anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1000, blit=False,frames=np.arange(10,0,-1))
plt.show()

Python matplotlib.animation Jupyter Notebook

I use Windows 10 / 64 / Google chrome
I found a good set-up for animation over Jupyter with the call %matplotlib notebook as here :
import numpy as np
import scipy.stats as st
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
For exemple, this one is working pretty well :
n = 100
X = st.norm(0,1).rvs(200)
number_of_frames = np.size(X)
def update_hist(num, second_argument):
plt.cla()
plt.hist(X[:num], bins = 20)
plt.title("{}".format(num))
plt.legend()
fig = plt.figure()
hist = plt.hist(X)
ani = animation.FuncAnimation(fig, update_hist, number_of_frames, fargs=(X, ), repeat = False )
plt.show()
But, weirdly the code below doesn't work while it's the same structure, it puzzles me :
X = np.linspace(-5,5, 150)
number_of_frames = np.size(X)
N_max = 100
N = np.arange(1,N_max+1)
h = 1/np.sqrt(N)
def update_plot(n, second_argument):
#plt.cla()
plt.plot(X, [f(x) for x in X], c = "y", label = "densité")
plt.plot(X, [fen(sample_sort[:n],h[n],x) for x in X], label = "densité")
plt.title("n = {}".format(n))
fig = plt.figure(6)
plot = plt.plot(X, [f(x) for x in X], c = "y", label = "densité")
ani = animation.FuncAnimation(fig, update_plot, number_of_frames, fargs=(X, ), repeat = False )
plt.show()
Thanks for your help, best regards.
EDIT : You don't have the funciton fen(sample_sort[:n],h[n],x) it is a function from float to float taking a x in argument and returning a flot. The argument sample_sort[:n],h[n] it is just maths things I'm trying to understand some statistics anyway, you can remplace with line with what you want np.cos(N[:n]) for exemple.
EDIT : New code according to the suggestion :
N_max = 100
X = np.linspace(-5,5, N_max )
number_of_frames = np.size(X)
N = np.arange(1,N_max+1)
h = 1/np.sqrt(N)
def update_plot(n):
#plt.cla()
lines.set_data(X, np.array([fen(sample_sort[:n],h[n],x) for x in X]))
ax.set_title("n = {}".format(n))
return lines
fig = plt.figure()
ax = plt.axes(xlim=(-4, 4), ylim=(-0.01, 1))
ax.plot(X, np.array([f(x) for x in X]), 'y-', lw=2, label="d")
lines, = ax.plot([], [], 'b--', lw=3, label="f")
ani = animation.FuncAnimation(fig, update_plot, number_of_frames, repeat = False )
plt.show()
EDIT 2:
I found a code over internet which does exactly what I would like
# Fermi-Dirac Distribution
def fermi(E: float, E_f: float, T: float) -> float:
return 1/(np.exp((E - E_f)/(k_b * T)) + 1)
# Create figure and add axes
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
# Get colors from coolwarm colormap
colors = plt.get_cmap('coolwarm', 10)
# Temperature values
T = np.array([100*i for i in range(1,11)])
# Create variable reference to plot
f_d, = ax.plot([], [], linewidth=2.5)
# Add text annotation and create variable reference
temp = ax.text(1, 1, '', ha='right', va='top', fontsize=24)
# Set axes labels
ax.set_xlabel('Energy (eV)')
ax.set_ylabel('Fraction')
# Animation function
def animate(i):
x = np.linspace(0, 1, 100)
y = fermi(x, 0.5, T[i])
f_d.set_data(x, y)
f_d.set_color(colors(i))
temp.set_text(str(int(T[i])) + ' K')
temp.set_color(colors(i))
# Create animation
ani = animation.FuncAnimation(fig, animate, frames=range(len(T)), interval=500, repeat=False)
# Ensure the entire plot is visible
fig.tight_layout()
# show animation
plt.show()
What I want to draw is a curve at random because the actual state of the function is unknown. The basic structure looks like this, so please modify it based on this.
import numpy as np
import scipy.stats as st
# %matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# from IPython.display import HTML
# from matplotlib.animation import PillowWriter
X = np.linspace(-5,5, 100)
number_of_frames = np.size(X)
N_max = 100
N = np.arange(1,N_max+1)
h = 1/np.sqrt(N)
def update_plot(n):
#plt.cla()
lines.set_data(X[:n], h[:n])
lines2.set_data(X[:n], h[:n]*-1)
ax.set_title("n = {}".format(n))
return lines, lines2
fig = plt.figure()
ax = plt.axes(xlim=(-5, 5), ylim=(-1, 1))
lines, = ax.plot([], [], 'y-', lw=2, label="densité")
lines2, = ax.plot([], [], 'b--', lw=3, label="densité2")
ani = animation.FuncAnimation(fig, update_plot, frames=number_of_frames, repeat=False )
plt.show()
# ani.save('lines_ani2.gif', writer='pillow')
# plt.close()
# HTML(ani.to_html5_video())

How can I animate Pandas dataframe using matplotlib

I have a dataframe that I want to animate (line chart) using matplotlib. My x and y values:
here x = df.index and y = df['Likes']
x y
0 200000
1 50000
2 1000000
.so on.. ....
Code I tried:
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
df = pd.read_csv("C:\\Users\\usr\\Documents\\Sublime\\return_to_windows\\Files\\cod2019.txt", sep='\t')
fig = plt.figure()
ax = plt.axes(xlim=(0, 18), ylim=(6514, 209124))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
line.set_data(df.index[i], df['Likes'][i])
return line,
anim = animation.FuncAnimation(fig, animate, frames=len(df['Likes']), init_func=init, interval=300, blit=True)
plt.show()
I have tried this, but it is showing blank output with no error message. I am using python 3.83, windows machine. Can I do this using numpy? Almost all of the examples used numpy data in FuncAnimation.
I have solved it myself, I have used code of "vkakerbeck" from github as a guide to add more data points:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
df = pd.read_csv("C:\\Users\\usr\\Documents\\Sublime\\return_to_windows\\Files\\cod2019.txt", sep='\t')
dg = df['Likes']
x_data = []
y_data = []
fig, ax = plt.subplots()
ax.set_xlim(0, len(dg))
ax.set_ylim(0, dg.max() * 1.04) # multiplied with 1.04 to add some gap in y-axis
line, = ax.plot(0, 0)
This part is for formatting
ax.set_xlabel('Part No')
ax.set_ylabel('Number of Likes')
ax.set_title('Likes in Call of Duty 2019')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
fig = plt.gcf()
fig.set_size_inches(12.8, 7.2) # 720p output
I have used this from that guide to add more data points to make the animation less jumpy:
x = np.array(dg.index)
y = np.array(dg)
def augment(xold, yold, numsteps):
xnew = []
ynew = []
for i in range(len(xold) - 1):
difX = xold[i + 1] - xold[i]
stepsX = difX / numsteps
difY = yold[i + 1] - yold[i]
stepsY = difY / numsteps
for s in range(numsteps):
xnew = np.append(xnew, xold[i] + s * stepsX)
ynew = np.append(ynew, yold[i] + s * stepsY)
return xnew, ynew
XN, YN = augment(x, y, 3)
augmented = pd.DataFrame(YN, XN)
ylikes = augmented[0].reset_index() # Index reset to avoid key error
Main Function:
def animation_frame(i):
x_data.append(augmented.index[i])
y_data.append(ylikes[0][i])
line.set_xdata(x_data)
line.set_ydata(y_data)
return line,
plt.cla()
plt.tight_layout()
anima = animation.FuncAnimation(fig, func=animation_frame, frames=len(augmented), interval=80)
plt.show()
Export as mp4
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, bitrate=1000)
anima.save('lines3.mp4', writer=writer)

How can I get tight_layout() in matplotlib to work with inset plots?

I'm using matplotlib.pyplot to make a plot with several subplots. Here's what I ultimately want: a 2x2 array of "major" plots. Each of these have two curves in the plot, each using a different y axis. In addition, I want a smaller inset plot in each of these.
I've got the first part so far, using this working example code:
import matplotlib.pyplot as plt
import numpy as np
import os
import shutil
import time
import sys
#Simplest working example of tight_layout and plots problem
def two_scales(ax1, time, data1, data2, c1, c2, xlabel, y1label, y2label):
ax2 = ax1.twinx()
ax1.plot(time, data1, color=c1)
ax1.set_xlabel(xlabel)
ax1.set_ylabel(y1label)
ax2.plot(time, data2, color=c2)
ax2.set_ylabel(y2label)
return ax1, ax2
# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
for t in ax.get_yticklabels():
t.set_color(color)
return None
def insetPlots():
t = np.arange(0.01, 10.0, 0.01)
#Figure stuff
fig, baseAxes = plt.subplots(2,2,figsize=(10, 6))
baseAxesFlattened = baseAxes.flatten()
for i, dat in enumerate(baseAxesFlattened):
s1 = np.exp((i+1)*t)
s2 = .3*np.sin((i+1)*.2 * np.pi * t)
#Plotting them together
tempAx1, tempAx2 = two_scales(baseAxesFlattened[i], t, s1, s2, 'b', 'r','heyheyhey','yayaya','woopwoop')
#Changing the color of the axes
color_y_axis(tempAx1, 'b')
color_y_axis(tempAx2, 'r')
plt.tight_layout()
#plt.figure(figsize=(6, 8))
picname="/mypath/testtesttest.png"
plt.savefig(picname)
insetPlots()
Which produces this, good so far:
Now I want to add the insets. I can do this too fairly easily:
import matplotlib.pyplot as plt
import numpy as np
import os
import shutil
import time
import sys
#Simplest working example of tight_layout and plots problem
def two_scales(ax1, time, data1, data2, c1, c2, xlabel, y1label, y2label):
ax2 = ax1.twinx()
ax1.plot(time, data1, color=c1)
ax1.set_xlabel(xlabel)
ax1.set_ylabel(y1label)
ax2.plot(time, data2, color=c2)
ax2.set_ylabel(y2label)
return ax1, ax2
# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
for t in ax.get_yticklabels():
t.set_color(color)
return None
def insetPlots():
t = np.arange(0.01, 10.0, 0.01)
#Figure stuff
fig, baseAxes = plt.subplots(2,2,figsize=(10, 6))
baseAxesFlattened = baseAxes.flatten()
for i, dat in enumerate(baseAxesFlattened):
s1 = np.exp((i+1)*t)
s2 = .3*np.sin((i+1)*.2 * np.pi * t)
#Plotting them together
tempAx1, tempAx2 = two_scales(baseAxesFlattened[i], t, s1, s2, 'b', 'r','heyheyhey','yayaya','woopwoop')
#Changing the color of the axes
color_y_axis(tempAx1, 'b')
color_y_axis(tempAx2, 'r')
pos = tempAx1.get_position()
#print(pos)
posString = str(pos)
x0Ind, y0Ind, x1Ind, y1Ind = posString.find('x0'),posString.find('y0'),posString.find('x1'),posString.find('y1')
#print(x0Ind, y0Ind, x1Ind, y1Ind)
x0, y0, x1, y1 = float(posString[x0Ind+3:y0Ind-2]), float(posString[y0Ind+3:x1Ind-2]), float(posString[x1Ind+3:y1Ind-2]), float(posString[y1Ind+3:-1])
#print(x0, y0, x1, y1)
mainPlotW = x1 - x0
mainPlotH = y1 - y0
w, h = 0.3*mainPlotW, 0.25*mainPlotH
left, bottom, width, height = [x0 + .15*mainPlotW, y0 + .7*mainPlotH, w, h]
insetAx = fig.add_axes([left, bottom, width, height])
#insetAx.plot(range(6)[::-1], color='green')
s3 = np.sin(.2 * np.pi * t/(i+1))
insetAx.plot(t,s3, color='green')
#plt.tight_layout()
#plt.figure(figsize=(6, 8))
picname="/mypath/testtesttest.png"
plt.savefig(picname)
insetPlots()
Note that here I've commented out tight_layout(). This produces this, which has the inset plots in the positions I want them:
So this has the inset plots in the right positions, but because tight_layout() is gone, the axes labels for the major plots are overlapping. If I have tight_layout() (so, same exact code as directly above, but with that line uncommented), I get this:
Where the major plots' axes aren't overlapping anymore, but the insets are now in the wrong positions. I also get this warning when I run the code:
/usr/local/lib/python3.6/dist-packages/matplotlib/figure.py:2022: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
warnings.warn("This figure includes Axes that are not compatible "
How can I make them both work? I suspect I'm doing something simple wrong, like placing insets in the wrong way.
EDIT: I've found a solution, but it's ugly and I hope not the "proper" way to do it. I suspected that tight_layout() is moving things around, so the positions of the inset plots (which depend on the positions of the major plots) were getting messed up relative to the major plots after tight_layout(). So I solved the problem by plotting the major plots, doing tight layout, and then adding the inset ones:
import matplotlib.pyplot as plt
import numpy as np
import os
import shutil
import time
import sys
#Simplest working example of tight_layout and plots problem
def two_scales(ax1, time, data1, data2, c1, c2, xlabel, y1label, y2label):
ax2 = ax1.twinx()
ax1.plot(time, data1, color=c1)
ax1.set_xlabel(xlabel)
ax1.set_ylabel(y1label)
ax2.plot(time, data2, color=c2)
ax2.set_ylabel(y2label)
return ax1, ax2
# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
for t in ax.get_yticklabels():
t.set_color(color)
return None
def insetPlots():
t = np.arange(0.01, 10.0, 0.01)
#Figure stuff
fig, baseAxes = plt.subplots(2,2,figsize=(10, 6))
baseAxesFlattened = baseAxes.flatten()
majorAxes = []
for i, dat in enumerate(baseAxesFlattened):
s1 = np.exp((i+1)*t)
s2 = .3*np.sin((i+1)*.2 * np.pi * t)
#Plotting them together
tempAx1, tempAx2 = two_scales(baseAxesFlattened[i], t, s1, s2, 'b', 'r','heyheyhey','yayaya','woopwoop')
majorAxes.append(tempAx1)
#Changing the color of the axes
color_y_axis(tempAx1, 'b')
color_y_axis(tempAx2, 'r')
plt.tight_layout()
for i, dat in enumerate(baseAxesFlattened):
tempAx1 = majorAxes[i]
pos = tempAx1.get_position()
#print(pos)
posString = str(pos)
x0Ind, y0Ind, x1Ind, y1Ind = posString.find('x0'),posString.find('y0'),posString.find('x1'),posString.find('y1')
#print(x0Ind, y0Ind, x1Ind, y1Ind)
x0, y0, x1, y1 = float(posString[x0Ind+3:y0Ind-2]), float(posString[y0Ind+3:x1Ind-2]), float(posString[x1Ind+3:y1Ind-2]), float(posString[y1Ind+3:-1])
#print(x0, y0, x1, y1)
mainPlotW = x1 - x0
mainPlotH = y1 - y0
w, h = 0.3*mainPlotW, 0.25*mainPlotH
left, bottom, width, height = [x0 + .15*mainPlotW, y0 + .7*mainPlotH, w, h]
insetAx = fig.add_axes([left, bottom, width, height])
#insetAx.plot(range(6)[::-1], color='green')
s3 = np.sin(.2 * np.pi * t/(i+1))
insetAx.plot(t,s3, color='green')
#plt.tight_layout()
#plt.figure(figsize=(6, 8))
picname="/mypath/testtesttest.png"
plt.savefig(picname)
insetPlots()
Is there a cleaner way to do this?
tight_layout() is just a useful tool for most common plots, but it cannot deal with every situation.
In your particular case, I think you are better off calling tight_layout() before creating your inset axes, and using the resulting axes position to find the correct coordinates for your insets
import matplotlib.pyplot as plt
import numpy as np
import os
import shutil
import time
import sys
#Simplest working example of tight_layout and plots problem
def two_scales(ax1, time, data1, data2, c1, c2, xlabel, y1label, y2label):
ax2 = ax1.twinx()
ax1.plot(time, data1, color=c1)
ax1.set_xlabel(xlabel)
ax1.set_ylabel(y1label)
ax2.plot(time, data2, color=c2)
ax2.set_ylabel(y2label)
return ax1, ax2
# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
for t in ax.get_yticklabels():
t.set_color(color)
return None
def insetPlots():
t = np.arange(0.01, 10.0, 0.01)
#Figure stuff
fig, baseAxes = plt.subplots(2,2,figsize=(10, 6))
baseAxesFlattened = baseAxes.flatten()
for i, ax in enumerate(baseAxesFlattened):
s1 = np.exp((i+1)*t)
s2 = .3*np.sin((i+1)*.2 * np.pi * t)
#Plotting them together
tempAx1, tempAx2 = two_scales(ax, t, s1, s2, 'b', 'r','heyheyhey','yayaya','woopwoop')
#Changing the color of the axes
color_y_axis(tempAx1, 'b')
color_y_axis(tempAx2, 'r')
fig.tight_layout()
for i, ax in enumerate(baseAxesFlattened):
pos = ax.get_position()
#print(pos)
mainPlotW = pos.x1 - pos.x0
mainPlotH = pos.y1 - pos.y0
w, h = 0.3*mainPlotW, 0.25*mainPlotH
left, bottom, width, height = [pos.x0 + .15*mainPlotW, pos.y0 + .7*mainPlotH, w, h]
insetAx = fig.add_axes([left, bottom, width, height])
insetAx.plot(range(6)[::-1], color='green')
s3 = np.sin(.2 * np.pi * t/(i+1))
insetAx.plot(t,s3, color='green')
insetPlots()
PS You're doing some pretty weird things with the pos variable, transforming it to str before casting it back to float. I've simplified your code in the second loop in my code
I would suggest using mpl_toolkits.axes_grid1.inset_locator.InsetPosition to position the inset. This simplifies things a lot, not needing to multiply plot sizes with anything.
You may then choose to call fig.tight_layout() before or after creating the insets, the resulting plot will not change (though calling it after gives a warning, which you can ignore in this case).
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition
import numpy as np
def two_scales(ax1, time, data1, data2, c1, c2, xlabel, y1label, y2label):
ax2 = ax1.twinx()
ax1.plot(time, data1, color=c1)
ax1.set_xlabel(xlabel)
ax1.set_ylabel(y1label)
ax2.plot(time, data2, color=c2)
ax2.set_ylabel(y2label)
return ax1, ax2
# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
for t in ax.get_yticklabels():
t.set_color(color)
return None
def insetPlots():
t = np.arange(0.01, 10.0, 0.01)
#Figure stuff
fig, baseAxes = plt.subplots(2,2,figsize=(10, 6))
baseAxesFlattened = baseAxes.flatten()
for i, ax in enumerate(baseAxesFlattened):
s1 = np.exp((i+1)*t)
s2 = .3*np.sin((i+1)*.2 * np.pi * t)
#Plotting them together
tempAx1, tempAx2 = two_scales(ax, t, s1, s2, 'b', 'r',
'heyheyhey','yayaya','woopwoop')
#Changing the color of the axes
color_y_axis(tempAx1, 'b')
color_y_axis(tempAx2, 'r')
fig.tight_layout()
for i, ax in enumerate(baseAxesFlattened):
insetAx = fig.add_axes([0, 0, 1, 1], label="{}".format(i))
ip = InsetPosition(ax, [.15, 0.7, 0.3, 0.25]) #posx, posy, width, height
insetAx.set_axes_locator(ip)
insetAx.plot(range(6)[::-1], color='green')
s3 = np.sin(.2 * np.pi * t/(i+1))
insetAx.plot(t,s3, color='green')
# putting tight_layout here will produce a warning,
# yet the resulting plot is the same
# fig.tight_layout()
insetPlots()
plt.show()

Categories