I'm trying to plot two lines with different scales with matplotlib.
It is currently working, except when I run my code the second Y axis messes up while updating.
Here is the code I'm using:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.pyplot import cm
from datetime import datetime
import numpy as np
import matplotlib.animation as animation
def animate(i, fig, ax):
# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%d-%m-%Y_%H:%M:%S'))
# Read data from 'file.dat'
dates, levels, temp = np.genfromtxt('datosPlot.txt', # Data to be read
converters={0: datefunc}, # Formatting of column 0
dtype=float, # All values are floats
usecols=(0,1,2), #Leer las tres primeras columnas de datos.txt
unpack=True) # Unpack to several variables
# Configure x-ticks
ax1.clear()
ax1.set_xticks(dates) # Tickmark + label at every plotted point
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax2 = ax1.twinx()
fig.tight_layout()
fig = plt.figure()
ax1 = fig.add_subplot(111)
ani = animation.FuncAnimation(fig, animate, fargs=(fig, ax1), interval=1000)
plt.show()
My data (datosPlot.txt) look like this:
14-01-2017_14:01:16 1 16
14-01-2017_14:01:19 14 22
14-01-2017_14:01:22 2 17
14-01-2017_14:01:25 4 19
14-01-2017_14:01:28 6 24
14-01-2017_14:01:31 12 19
14-01-2017_14:01:34 4 18
14-01-2017_14:01:37 9 20
First column is the X axis (date_time), second column is pH, third column is Temperature.
I've tried adding a ax2.clear() before and after calling ax2 = ax1.twinx(), but it doesn't work. How can I clear it, as I'm able with ax1?
Here is what it looks like when I don't add any ax2.clear():
Try to create the axes outside of your animation and only use as little code as you really need in each animation step.
The following is a runnable example, where you would need to replace the read in function em.genfromtxt() with your original call to np.genfromtxt(....).
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import numpy as np
import matplotlib.animation as animation
##### Emulator to generate data #########
class emulator():
def __init__(self):
self.dates = []
self.levels = []
self.temp = []
def genfromtxt(self):
self.dates.append(mdates.date2num(datetime.now()))
self.levels.append(np.random.randint(1,14))
self.temp.append(np.random.rand(1)*16+4)
return self.dates, self.levels, self.temp
em = emulator()
##### End of Emulator to generate data #########
# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%d-%m-%Y_%H:%M:%S'))
def animate(i):
# Read data from 'file.dat'
# instead we use an emulator here, replace with your original genfromtxt function
dates, levels, temp = em.genfromtxt()
# Configure x-ticks
ax1.clear()
ax2.clear()
ax1.grid(True)
ax2.grid(True)
ax1.plot_date(dates, levels, ls='-', marker='.', color='red', label='pH')
ax2.plot_date(dates, temp, ls='-', marker='.', color='blue', label='Temperatura C')
ax1.set_xticks(dates) # Tickmark + label at every plotted point
ax1.locator_params(axis='x',nbins=10)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
#Leyendas
lines = ax1.get_lines() + ax2.get_lines()
plt.legend(lines, [l.get_label() for l in lines], loc=2)
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
fig = plt.figure()
# we create both axes outside the animation and already set those parameters
# which stay the same throughout the animation.
ax1 = fig.add_subplot(111)
ax1.set_title('pH y Temp')
ax1.set_ylabel('pH')
ax2 = ax1.twinx() # This should happen outside the animation already.
ax2.set_ylabel('Temperatura C')
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Related
I am trying to write a code that will generate a graph that is being repeatedly updated and has twin axes (2 y-axis, sharing the same x-axis).
The code works well when I don't combine it with FuncAnimation, however when I try to do that I get an empty graph.
def animate(i):
data=prices(a,b,c) #function that gives a DataFrame with 2 columns and index
plt.cla()
fig=plt.figure()
ax = fig.add_subplot(111)
ax.plot(data.index, data.value1)
ax2 = ax.twinx()
ax2.plot(data.index, data.value2)
plt.gcf().autofmt_xdate()
plt.tight_layout()
call = FuncAnimation(plt.gcf(), animate, 1000)
plt.tight_layout()
plt.show
'''
I believe the error is in "call". Unfortunately, I don't know FuncAnimation so well.
You can try something like this:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
from datetime import datetime, timedelta
def getPrices(i):
return pd.DataFrame(index=[datetime.now() + timedelta(hours=i) for i in range(10)], data={'value1':range(10), 'value2':[(x + i) % 5 for x in range(10)]})
def doAnimation():
fig=plt.figure()
ax = fig.add_subplot(111)
def animate(i):
#data=prices(a,b,c) #function that gives a DataFrame with 2 columns and index
data = getPrices(i)
plt.cla()
ax.plot(data.index, data.value1)
ax2 = ax.twinx()
ax2.plot(data.index, data.value2)
plt.gcf().autofmt_xdate()
plt.tight_layout()
return ax, ax2
call = FuncAnimation(plt.gcf(), animate, 1000)
plt.show()
doAnimation()
UPDATE:
Though this works in my environment, OP in a comment indicated it doesn't work and the following warning is raised:
UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. anim, that exists until you have outputted the Animation using plt.show() or anim.save()
As plt.show() is called immediately after the call to FuncAnimation(), this is puzzling, but perhaps the following will help to ensure the Animation does not get deleted prematurely:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
from datetime import datetime, timedelta
def getPrices(i):
return pd.DataFrame(index=[datetime.now() + timedelta(hours=i) for i in range(10)], data={'value1':range(10), 'value2':[(x + i) % 5 for x in range(10)]})
def doAnimation():
fig=plt.figure()
ax = fig.add_subplot(111)
def animate(i):
#data=prices(a,b,c) #function that gives a DataFrame with 2 columns and index
data = getPrices(i)
plt.cla()
ax.plot(data.index, data.value1)
ax2 = ax.twinx()
ax2.plot(data.index, data.value2)
plt.gcf().autofmt_xdate()
plt.tight_layout()
return ax, ax2
call = FuncAnimation(plt.gcf(), animate, 1000)
return call
callSave = doAnimation()
plt.show()
I have this code to plot
import matplotlib.pyplot as plt
import numpy as np
# Data
time = np.arange(0,20,0.05)
data = np.sin(time)
# Plot
main_plot = plt.plot(time, data)
error_plot = plt.fill_between(time, data+0.5, data-0.5, alpha=0.2)
plt.show()
and I would like to plot, after in the code, the same plot but using the main_plot and error_plot variable. So I have this
plt.plot(*main_plot[0].get_data())
# Here I want to plot the error_plot (PolyCollection type)
# but I don't know how. Something like
# plt.plot(error_plot)
I have tried this related post with no result. I've also tried
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_collection(error_plot)
but resulting in this error
1 fig = plt.figure()
2 ax = fig.add_subplot(111)
----> 3 ax.add_collection(error_plot)
RuntimeError: Can not put single artist in more than one figure
I'm currently reading data points from a CSV file every 2 seconds and plotting it using matplotlib Funcanimation. However, the date ticks on the x-axis are stacking on top of each other and are therefore unreadable. I'm looking for an efficient way to arrange the x-ticks so they don't stack on top of each other.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
def animate(i):
data = pd.read_csv('data.csv')
x = data.iloc[:,0]
y4 = data.iloc[:,4]
plt.cla()
plt.plot(x, y4, label = "value")
plt.legend(loc= 'upper left')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval = 2000)
plt.show()
Assuming that your data is in a date format, e.g. np.datetime64, Have you tried the ConciseDateFormatter from the documentation?
for your example this would be something like
import matplotlib.dates as mdates
def animate(i):
data = pd.read_csv('data.csv')
x = data.iloc[:,0]
y4 = data.iloc[:,4]
plt.cla()
ax = plt.gca()
locator = mdates.AutoDateLocator(minticks=3, maxticks=7)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.plot(x, y4, label = "value")
plt.legend(loc= 'upper left')
plt.tight_layout()
I am new to python and am carrying out some little projects along side watching tutorials to enable me to learn.
I have recently been working with some APIs to collect data - I save this data in a CSV file and then open the CSV file to show the data as a graph.
I want the graph to show the data LIVE, but in doing so I only want 10 values on the screen at once, so when the 11th value is plotted, the 1st is no longer visible unless the scrolling function is used to look back at it..
I have managed to pull together the code that plots the live data from the CSV file, as well as some code that creates the graph in the desired format - but as I am quite new to python I am unsure of how I'd make them work together.. Any advice would be greatly appreciated.
Below is the code that I have created to read and plot from a CSV file:
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
index = count()
def animate(i):
data = pd.read_csv('x.csv')
x = data['Time']
y = data['R1Temp']
y1 = data['R2Temp']
y2 = data['R3Temp']
plt.cla()
plt.plot(x, y, marker = 'o', label='Room 1 Temp')
plt.plot(x, y1, marker = 'o', label='Room 2 Temp')
plt.plot(x, y2, marker = 'o', label='Room 3 Temp')
plt.xlabel("Time")
plt.ylabel("Temperature °C")
plt.title("Live temperature of Rooms")
plt.legend(loc='upper left')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.tight_layout()
plt.show()
Below is the code that shows the way in which I'd like the graph to format the data plots:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update(frame):
global x, y
start = x[max(frame-PAN//2, 0)]
start = x[max(frame-PAN+1, 0)]
end = start + PAN
ax.set_xlim(start, end)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, TICK))
ax.figure.canvas.draw()
line1.set_data(x[0:frame+1], y[0:frame+1])
return (line1,)
# main
NUM = 100
TICK = 1
PAN = 10
x = np.arange(start=1, stop=NUM + 1, step=1)
for i in range(NUM):
y = np.random.rand(NUM) * 100
fig, ax = plt.subplots()
ax.set_xlim(0, PAN)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, TICK))
ax.set_ylim(0, 100)
line1, = ax.plot([], [], color="r")
ani = animation.FuncAnimation(fig, update, frames=len(x), interval=1000, repeat=False)
plt.show()
I have tried many ways to merge them together, but I just cant seem to find the correct way to go about it.
Thanks in advance!!
Showing the last N time points is quite easy. Just use DataFrame.tail() to get the last N rows of your dataframe.
Note that when doing an animation, the recommended way is to create your axes and artists outside the animation code, and only update your artists' data inside the animate code.
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
l1, = ax.plot([], [], marker='o', label='Room 1 Temp')
l2, = ax.plot([], [], marker='o', label='Room 2 Temp')
l3, = ax.plot([], [], marker='o', label='Room 3 Temp')
plt.xlabel("Time")
plt.ylabel("Temperature °C")
plt.title("Live temperature of Rooms")
plt.legend(loc='upper left')
plt.tight_layout()
def animate(i, N):
data = pd.read_csv('x.csv').tail(N)
l1.set_data(data['Time'], data['R1Temp'])
l2.set_data(data['Time'], data['R2Temp'])
l3.set_data(data['Time'], data['R3Temp'])
ax.relim()
ax.autoscale_view()
return l1, l2, l3
ani = FuncAnimation(fig, animate, interval=1000, frames=None, fargs=(10,))
plt.show()
I'm getting data from serial port and draw it with matplotlib. But there is a problem. It is that i cannot order y axis values.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from deneme_serial import serial_reader
collect = serial_reader()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs=[]
ys=[]
def animate(i, xs, ys):
xs = collect.collector()[0]
ys = collect.collector()[1]
ax.clear()
ax.plot(xs)
ax.plot(ys)
axes=plt.gca()
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('TMP102 Temperature over Time')
plt.ylabel('Temperature (deg C)')
ani = animation.FuncAnimation(fig, animate, fargs=(xs,ys), interval=1000)
plt.show()
Below graph is result of above code
This happened to me following the same tutorial.
My issue was the variables coming from my instrument were strings. Therefore, there is no order. I changed my variables to float and that fixed the problem
xs.append(float(FROM_INSTRUMENT))