Python matplotlib stepped axis label - python

I am using matplotlib to generate an image in a tkinter application. The data provides a value stored against a time (real app data is open sessions by second to show load). I am trying to display this with time across the x axis, and sessions up the y axis.
I am having some difficulty however with formatting the labels on the x axis. If you see image Test 1, if i assign integers to the x axis, then although data is 0-19, matplotlib automatically displays 0,5,10,15, to keep the axis tidy.
If I just assign labels to this, then the first 4 labels are shown rather than every 5th label. If I reset the tciks and labels, I get every tick and every label (which just about fits here but in my real app theres too much data).
My solution has been to manually calculate every Nth tick and Nth label and assign those values which has worked but seems like there should be some built in functionality that handles this sort of data, the same way as it does for integers.
My code is:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
import datetime
import tkinter
def main():
x_labels = []
x = []
y = []
dt_now = datetime.datetime.now()
entries = 20
for i in range(0, entries):
newtime=(dt_now + datetime.timedelta(seconds=i)).strftime("%H:%M:%S")
x.append(i)
y.append(i/2)
x_labels.append(newtime)
root = tkinter.Tk()
fig = plt.figure(figsize=(8,8))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
ax1 = fig.add_subplot(221)
ax1.plot(x,y)
ax1.set_title('Test 1 - Original Values')
ax1.set_xlabel('Entry ID')
ax1.set_ylabel('Sessions')
ax2 = fig.add_subplot(222)
ax2.plot(x,y)
ax2.set_title('Test 2 - Labels 0,1,2,3\nExpecting labels 0,5,10,15')
ax2.set_xlabel('Entry Time')
ax2.set_ylabel('Sessions')
ax2.set_xticklabels(x_labels, rotation=90, ha='center')
ax3 = fig.add_subplot(223)
ax3.plot(x_labels,y)
ax3.set_title('Test 3 - Every label')
ax3.set_xlabel('Entry Time')
ax3.set_ylabel('Sessions')
ax3.set_xticklabels(x_labels, rotation = 90)
major_ticks = []
major_tick_labels = []
for i in range(0,entries,int(entries/5)):
major_ticks.append(x[i])
major_tick_labels.append(x_labels[i])
ax4 = fig.add_subplot(224)
ax4.plot(x,y)
ax4.set_title('Test 4 - What I''m expecting\nbut hard coded')
ax4.set_xlabel('Entry Time')
ax4.set_ylabel('Sessions')
ax4.set_xticks(major_ticks)
ax4.set_xticklabels(major_tick_labels, rotation=90, ha='center')
plt.subplots_adjust(hspace = 0.75, bottom = 0.2)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
if __name__ == '__main__':
main()
Which generates the following:
Is this the only way to achieve the requirement or is there something available that I am missing. I have read the documentation, but couldn't see anything relevant in there (past making sure I set ticks and labels together). I'd like the process to be as automated as possible, as the time frame will be user driven, so may be best with 4 ticks, 5 ticks 10 ticks, etc. Matplotlib seems to handle this requirement well the a defined range of integers, I just want to associate the same labels.

You could use matplotlib.dates. Note that you need to keep the x_labels formatted as datetimes for this to work (I removed your call to .strftime)
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x_labels = []
x = []
y = []
dt_now = datetime.datetime.now()
entries = 20
for i in range(0, entries):
newtime=(dt_now + datetime.timedelta(seconds=i))
x.append(i)
y.append(i/2)
x_labels.append(newtime)
fig = plt.figure(figsize=(8,8))
ax4 = fig.add_subplot(224)
ax4.plot(x_labels,y)
ax4.set_title('One possible solution')
ax4.set_xlabel('Entry Time')
ax4.set_ylabel('Sessions')
ax4.xaxis.set_major_locator(mdates.SecondLocator(interval=4))
ax4.tick_params(axis="x", rotation=90)
This will give you

Related

Newlines in X Axis in Matplotlib Cause Undesired Window Resizing/Jumping/Flickering Behavior

I am plotting dates and times on the x axis in matplotlib. Because I want to plot as many labels as possible, I am using newlines in the x labels like so:
Unfortunately, this has the side effect of resizing the matplotlib window when I hover over the graph since it tries to print the x value, which contains the newlines, at the bottom.
See this video here as a demonstration:
YouTube Link to Video, watch in full resolution.
It even causes the whole chart to flicker sometimes, which doesn't exactly make it fun to interact with. I don't want to get rid of the toolbar, but I just need it to not print the x point in the bottom right corner (which is what's causing the window to resize).
Any idea how I can keep the toolbar but avoid this jumping/flickering issue? My code is below:
import os
import pandas
import matplotlib.pyplot as mp
import matplotlib.dates as md
import numpy as np
import datetime as dt
import time
for entry in os.scandir('estimated_finish_times'):
if entry.name.endswith('.csv'):
print(entry.name)
df = pandas.read_csv(entry.path)
df['lookahead_finish'] = df['polltime'] + df['lookahead_time']
df['combined_rate_finish'] = df['polltime'] + df['combined_rate_time']
xd = [dt.datetime.fromtimestamp(ts) for ts in df['polltime']]
x1 = md.date2num(xd)
yd = [dt.datetime.fromtimestamp(ts) for ts in df['lookahead_finish']]
y1 = md.date2num(yd) # df['lookahead_finish']
yd = [dt.datetime.fromtimestamp(ts) for ts in df['combined_rate_finish']]
y2 = md.date2num(yd) # df['lookahead_finish']
fig, ax = mp.subplots(figsize=(22, 11))
yfmt = md.DateFormatter('%b. %d, %Y at %I:%M %p')
xfmt = md.DateFormatter('%b. %d\n%I:%M\n%p\n%Y')
ax.xaxis.set_major_formatter(xfmt)
ax.xaxis.set_major_locator(mp.MaxNLocator(20))
ax.yaxis.set_major_formatter(yfmt)
ax.yaxis.set_major_locator(mp.MaxNLocator(20))
mp.plot(x1, y1, linewidth=2, label='lookahead_finish', marker='.', alpha=0.5)
mp.plot(x1, y2, linewidth=2, label='combined_rate_finish', marker='.', alpha=0.3)
mp.legend(bbox_to_anchor=(1.11, 1.0), loc="upper right")
mp.title(f'{entry.name} Estimated Finish Time')
mp.grid()
# fig.canvas.toolbar.pack_forget()
mp.show()
Note that every column in the dataframe is just unix timestamps (a value such as 1665123089, which is dtype: int64). Also, I'm on Windows 10, Python 3.8.2, and matplotlib==3.2.1.
Rather than hide the toolbar, which has been a suggested solution, I would still like the controls to be accessible if possible:
I don't want to get rid of the toolbar, but I just need it to not print the x point in the bottom right corner (which is what's causing the window to resize).
You can monkeypatch ax.format_coord to display a differently formatted string for a given x, y value (or None as below).
fig, ax = plt.subplots()
ax.format_coord = lambda x, y : '' # don't display anything
plt.show()
If you want to modify the string of the original ax.format_coord, then you have to wrap the original function:
fig, ax = plt.subplots()
old_function = ax.format_coord
ax.format_coord = lambda x, y: old_function(x, y) + "some random nonsense"
plt.show()

matplotlib: Second empty window on plt.show()

I usually don't ask questions on this platform, but I have a problem that quite bugs me.
Context
I have a function that plots data from a dataframe that has stockdata. It all works perfectly except for the fact that a second, empty window shows next to the actual graph whenever I execute this function. (image)
Here is all the relevant code, I'd be very grateful if some smart people could help me.
def plot(self):
plt.clf()
plt.cla()
colors = Colors()
data = self.getStockData()
if data.empty:
return
data.index = [TimeData.fromTimestamp(x) for x in data.index]
current, previous = data.iloc[-1, 1], data.iloc[0, 1]
percentage = (current / previous - 1) * 100
# Create a table
color = colors.decideColorPct(percentage)
# Create the table
fig = plt.figure(edgecolor=colors.NEUTRAL_COLOR)
fig.patch.set_facecolor(colors.BACKGROUND_COLOR)
plt.plot(data.close, color=color)
plt.title(self.__str2__(), color=colors.NEUTRAL_COLOR)
plt.ylabel("Share price in $", color=colors.NEUTRAL_COLOR)
plt.xlabel("Date", color=colors.NEUTRAL_COLOR)
ax = plt.gca()
ax.xaxis.set_major_formatter(plt_dates.DateFormatter('%Y/%m/%d %H:%M'))
ax.set_xticks([data.index[0], data.index[-1]])
ax.set_facecolor(colors.BACKGROUND_COLOR)
ax.tick_params(color=colors.NEUTRAL_COLOR, labelcolor=colors.NEUTRAL_COLOR)
for spine in ax.spines.values():
spine.set_edgecolor(colors.NEUTRAL_COLOR)
ax.yaxis.grid(True, color=colors.NEUTRAL_COLOR, linestyle=(0, (5, 10)), linewidth=.5)
plt.show()
Some notes:
Matplotlib never gets used in the program before this.
The data is standardized and consists of the following columns: open, low, high, close, volume.
The index of the dataframe exists of timestamps, which gets converted to an index of datetime objects at the following line: data.index = [TimeData.fromTimestamp(x) for x in data.index]
Remove plt.clf() and plt.cla() because it automatically creates window for plot when you don't have this window.
And later fig = plt.figure() creates new window which it uses to display your plot.
Minimal code for test
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame({'x': [1,2,3], 'y': [2,3,1]})
#plt.clf()
#plt.cla()
fig = plt.figure()
plt.plot(data)
ax = plt.gca()
plt.show()

python matplotlib shared xlabel description / title on multiple subplots for animation

I'm using the following code to produce an animation with matplotlib that is intended to visualize my experiments.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation, PillowWriter
plt.rcParams['animation.html'] = 'jshtml'
def make_grid(X, description=None, labels=None, title_fmt="label: {}", cmap='gray', ncols=3, colors=None):
L = len(X)
nrows = -(-L // ncols)
frame_plot = []
for i in range(L):
plt.subplot(nrows, ncols, i + 1)
im = plt.imshow(X[i].squeeze(), cmap=cmap, interpolation='none')
if labels is not None:
color = 'k' if colors is None else colors[i]
plt.title(title_fmt.format(labels[i]), color=color)
plt.xticks([])
plt.yticks([])
frame_plot.append(im)
return frame_plot
def animate_step(X):
return X ** 2
n_splots = 6
X = np.random.random((n_splots,32,32,3))
Y = X
X_t = []
for i in range(10):
Y = animate_step(Y)
X_t.append((Y, i))
frames = []
for X, step in X_t:
frame = make_grid(X,
description="step={}".format(step),
labels=range(n_splots),
title_fmt="target: {}")
frames.append(frame)
anim = ArtistAnimation(plt.gcf(), frames,
interval=300, repeat_delay=8000, blit=True)
plt.close()
anim.save("test.gif", writer=PillowWriter())
anim
The result can be seen here:
https://i.stack.imgur.com/OaOsf.gif
It works fine so far, but I'm having trouble getting a shared xlabel to add a description for all of the 6 subplots in the animation. It is supposed to show what step the image is on, i.e. "step=5".
Since it is an animation, I cannot use xlabel or set_title (since it would be constant over the whole animation) and have to draw the text myself.
I've tried something along the lines of..
def make_grid(X, description=None, labels=None, title_fmt="label: {}", cmap='gray', ncols=3, colors=None):
L = len(X)
nrows = -(-L // ncols)
frame_plot = []
desc = plt.text(0.5, .04, description,
size=plt.rcparams["axes.titlesize"],
ha="center",
transform=plt.gca().transAxes
)
frame_plot.append(desc)
...
This, of course, won't work, because the axes are not yet created. I tried using the axis of another subplot(nrows, 1, nrows), but then the existing images are drawn over..
Does anyone have a solution to this?
Edit:
unclean, hacky solution for now:
Wait for the axes of the middle image of the last row to be created and use that for plotting the text.
In the for loop:
...
if i == int((nrows - 0.5) * ncols):
title = ax.text(0.25, -.3, description,
size=plt.rcParams["axes.titlesize"],
# ha="center",
transform=ax.transAxes
)
frame_plot.append(title)
...
To me, your case is easier to solve with FuncAnimation instead of ArtistAnimation, even if you already have access to the full list of data you want to show animated (see this thread for a discussion about the difference between the two functions).
Inspired from this FuncAnimation example, I wrote the code below that does what you needed (using the same code with ArtistAnimation and correct list of arguments does not work).
The main idea is to initialize all elements to be animated at the beginning, and to update them over the animation frames. This can be done for the text object (step_txt = fig.text(...)) in charge of displaying the current step, and for the images out from ax.imshow. You can then update whatever object you would like to see animated with this recipe.
Note that the technique works if you want the text to be an x_label or any text you choose to show. See the commented line in the code.
#!/Users/seydoux/anaconda3/envs/jupyter/bin/python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
# parameters
n_frames = 10
n_splots = 6
n_cols = 3
n_rows = n_splots // n_cols
def update_data(x):
return x ** 2
# create all snapshots
snapshots = [np.random.rand(n_splots, 32, 32, 3)]
for _ in range(n_frames):
snapshots.append(update_data(snapshots[-1]))
# initialize figure and static elements
fig, axes = plt.subplots(2, 3)
axes = axes.ravel() # so we can access all axes with a single index
for i, ax in enumerate(axes):
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("target: {}".format(i))
# initialize elements to be animated
step_txt = fig.text(0.5, 0.95, "step: 0", ha="center", weight="bold")
# step_txt = axes[4].set_xlabel("step: 0") # also works with x_label
imgs = list()
for a, s in zip(axes, snapshots[0]):
imgs.append(a.imshow(s, interpolation="none", cmap="gray"))
# animation function
def animate(i):
# update images
for img, s in zip(imgs, snapshots[i]):
img.set_data(s)
# update text
step_txt.set_text("step: {}".format(i))
# etc
anim = FuncAnimation(fig, animate, frames=n_frames, interval=300)
anim.save("test.gif", writer=PillowWriter())
Here is the output I got from the above code:

Add a legend for an animation (of Artists) in matplotlib

I have made an animation from a set of images like this (10 snapshots):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import time
infile = open ('out.txt')
frame_counter = 0
N_p = 100
N_step = 10
N_line = N_p*N_step
for s in xrange(N_step):
x, y = [], []
for i in xrange(N_p):
data = infile.readline()
raw = data.split()
x.append(float(raw[0]))
y.append(float(raw[1]))
xnp = np.array(x)
ynp = np.array(y)
fig = plt.figure(0)
ax = fig.add_subplot(111, aspect='equal')
for x, y in zip(xnp, ynp):
cir = Circle(xy = (x, y), radius = 1)
cir.set_facecolor('red')
ax.add_artist(cir)
cir.set_clip_box(ax.bbox)
ax.set_xlim(-10, 150)
ax.set_ylim(-10, 150)
fig.savefig("step.%04d.png" % frame_counter)
ax.remove()
frame_counter +=1
Now I want to add a legend to each image showing the time step.
For doing this I must set legends to each of these 10 images. The problem is that I have tested different things like ax.set_label , cir.set_label, ...
and I get errors like this:
UserWarning: No labelled objects found. Use label='...' kwarg on individual plots
According to this error I must add label to my individual plots, but since this is a plot of Artists, I don't know how I can do this.
If for whatever reason you need a legend, you can show your Circle as the handle and use some text as the label.
ax.legend(handles=[cir], labels=["{}".format(frame_counter)])
If you don't really need a legend, you can just use some text to place inside the axes.
ax.text(.8,.8, "{}".format(frame_counter), transform=ax.transAxes)

Is it possible to generate a chart with this very specific background?

I need to create a chart, that has a grid like in the following picture.
The key factors being:
The x-axis is time with each tick marking 30 seconds
y-axes labels in the chart repeat at a variable interval
Chart must grow with the amount of data (i.e. for 30 minutes of data, it should be 60 boxes wide)
I have been looking into matplotlib for a bit, and it seems promising. I also managed to fill the chart with data. See my result for 40 Minutes of data.
But before I invest more time into research, I must know if this goal is even possible. If not I'll have to look into other charts. Thanks for your help!
Here is the source for the above image (my_data is actually read from a csv, but filled with random junk here):
from matplotlib import dates
import matplotlib.pyplot as plt
import numpy as np
import time
from datetime import datetime
my_data = list()
for i in range(3000):
my_data.append((datetime.fromtimestamp(i + time.time()), np.random.randint(50, 200), np.random.randint(10, 100)))
hfmt = dates.DateFormatter('%H:%M:%S')
fig = plt.figure()
actg = fig.add_subplot(2, 1, 1) # two rows, one column, first plot
plt.ylim(50, 210)
atoco = fig.add_subplot(2, 1, 2) # second plot
plt.ylim(0, 100)
actg.xaxis.set_minor_locator(dates.MinuteLocator())
actg.xaxis.set_major_formatter(hfmt)
atoco.xaxis.set_minor_locator(dates.MinuteLocator())
atoco.xaxis.set_major_formatter(hfmt)
plt.xticks(rotation=45)
times = []
fhr1 = []
toco = []
for key in my_data:
times.append(key[0])
fhr1.append(key[1])
toco.append(key[2])
actg.plot_date(times, fhr1, '-')
atoco.plot_date(times, toco, '-')
for ax in fig.axes:
ax.grid(True)
plt.tight_layout()
plt.show()
OK, here's something close to what you are after, I think.
I've used dates.SecondLocator(bysecond=[0,30]) to set the grid every 30 seconds (also need to make sure the grid is set on the minor ticks, with ax.xaxis.grid(True,which='both')
To repeat the yticklabels, I create a twinx of the axes for every major tick on the xaxis, and move the spine to that tick's location. I then set the spine color to none, so it doesn't show up, and turn of the actual ticks, but not the tick labels.
from matplotlib import dates
import matplotlib.pyplot as plt
import numpy as np
import time
from datetime import datetime
# how often to show xticklabels and repeat yticklabels:
xtickinterval = 10
# Make random data
my_data = list()
for i in range(3000):
my_data.append((datetime.fromtimestamp(i + time.time()), np.random.randint(120, 160), np.random.randint(10, 100)))
hfmt = dates.DateFormatter('%H:%M:%S')
fig = plt.figure()
actg = fig.add_subplot(2, 1, 1) # two rows, one column, first plot
actg.set_ylim(50, 210)
atoco = fig.add_subplot(2, 1, 2,sharex=actg) # second plot, share the xaxis with actg
atoco.set_ylim(-5, 105)
# Set the major ticks to the intervals specified above.
actg.xaxis.set_major_locator(dates.MinuteLocator(byminute=np.arange(0,60,xtickinterval)))
# Set the minor ticks to every 30 seconds
minloc = dates.SecondLocator(bysecond=[0,30])
minloc.MAXTICKS = 3000
actg.xaxis.set_minor_locator(minloc)
# Use the formatter specified above
actg.xaxis.set_major_formatter(hfmt)
times = []
fhr1 = []
toco = []
for key in my_data:
times.append(key[0])
fhr1.append(key[1])
toco.append(key[2])
print times[-1]-times[0]
# Make your plot
actg.plot_date(times, fhr1, '-')
atoco.plot_date(times, toco, '-')
for ax in [actg,atoco]:
# Turn off the yticklabels on the right hand side
ax.set_yticklabels([])
# Set the grids
ax.xaxis.grid(True,which='both',color='r')
ax.yaxis.grid(True,which='major',color='r')
# Create new yticklabels every major tick on the xaxis
for tick in ax.get_xticks():
tx = ax.twinx()
tx.set_ylim(ax.get_ylim())
tx.spines['right'].set_position(('data',tick))
tx.spines['right'].set_color('None')
for tic in tx.yaxis.get_major_ticks():
tic.tick1On = tic.tick2On = False
plt.tight_layout()
plt.show()

Categories