Updating legend entry using imshow in Python3 - python

I'm attempting to add a legend to overlay an imshow() plot displaying an animated array of random numbers. I want the legend to update to display the step that we are viewing.
I attempted to follow the steps here, which shows how to create an animated legend for subplots() using FuncAnimation. I believe the only way to display animated arrays is using ArtistAnimation() and imshow(), but one or both of these is causing me an issue to follow the linked solution.
I've attached below the working code to generate the animated random array, with the legend solution (from link) double commented out.
Any help or advice to remedy would be enormously appreciated.
Thanks,
C
import matplotlib.animation as animation
from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np
N=20
steps = 100
interval_pause = 100
repeat_pause = 1000
cmap = colors.ListedColormap(['white', 'black'])
bounds=[-1,0,1]
norm = colors.BoundaryNorm(bounds, cmap.N)
fig = plt.figure()
ax = plt.gca()
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
ax.axes.xaxis.set_ticks([])
ax.axes.yaxis.set_ticks([])
#plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[-1,0,1])
array = 2*(np.random.rand(N,N,steps)-0.5)
state = np.zeros(steps)
ims = []
##leg = ax.legend(loc='upper left',prop={'size':12})
for step in range(0,steps):
state = array[:,:,step]
im = plt.imshow(state,interpolation='nearest',cmap=cmap,norm=norm, animated=True)
##lab = 'step = '+str(step)
##leg.texts.set_text(lab)
ims.append([im])##+leg])
ani = animation.ArtistAnimation(fig,ims,interval=interval_pause,repeat_delay=repeat_pause)
#ani.save('animate_evolution '+str(timer())+'.mp4')
plt.show()

As shown in the question you link to it is easier to use a FuncAnimation. This allows to simply update a single legend and imshow plot instead of creating several of those.
Because it's not really clear what the legend is supposed to show for an imshow plot, I just created a blue rectangle. You can of course replace it with whatever you like.
import matplotlib.animation as animation
from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np
N=20
steps = 100
interval_pause = 100
repeat_pause = 1000
cmap = colors.ListedColormap(['white', 'black'])
bounds=[-1,0,1]
norm = colors.BoundaryNorm(bounds, cmap.N)
fig = plt.figure()
ax = plt.gca()
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
ax.axes.xaxis.set_ticks([])
ax.axes.yaxis.set_ticks([])
array = 2*(np.random.rand(N,N,steps)-0.5)
leg = ax.legend([plt.Rectangle((0,0),1,1)],["step0"], loc='upper left',prop={'size':12})
img = ax.imshow(array[:,:,0],interpolation='nearest',cmap=cmap,norm=norm, animated=True)
fig.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[-1,0,1])
def update(step):
state = array[:,:,step]
img.set_data(state)
lab = 'step = '+str(step)
leg.texts[0].set_text(lab)
ani = animation.FuncAnimation(fig,update,frames = steps,
interval=interval_pause,repeat_delay=repeat_pause)
plt.show()

Related

How do I remove double ticks (leaving a single set) on a color bar in matplotlib's imshow? [duplicate]

I'm making some interactive plots and I would like to add a colorbar legend. I don't want the colorbar to be in its own axes, so I want to add it to the existing axes. I'm having difficulties doing this, as most of the example code I have found creates a new axes for the colorbar.
I have tried the following code using matplotlib.colorbar.ColorbarBase, which adds a colorbar to an existing axes, but it gives me strange results and I can't figure out how to specify attributes of the colorbar (for instance, where on the axes it is placed and what size it is)
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.cm import coolwarm
import numpy as np
x = np.random.uniform(1, 10, 10)
y = np.random.uniform(1, 10, 10)
v = np.random.uniform(1, 10, 10)
fig, ax = plt.subplots()
s = ax.scatter(x, y, c=v, cmap=coolwarm)
matplotlib.colorbar.ColorbarBase(ax=ax, cmap=coolwarm, values=sorted(v),
orientation="horizontal")
Using fig.colorbar instead ofmatplotlib.colorbar.ColorbarBase still doesn't give me quite what I want, and I still don't know how to adjust the attributes of the colorbar.
fig.colorbar(s, ax=ax, cax=ax)
Let's say I want to have the colorbar in the top left corner, stretching about halfway across the top of the plot. How would I go about doing that?
Am I better off writing a custom function for this, maybe using LineCollection?
This technique is usually used for multiple axis in a figure. In this context it is often required to have a colorbar that corresponds in size with the result from imshow. This can be achieved easily with the axes grid tool kit:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = np.arange(100, 0, -1).reshape(10, 10)
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
im = ax.imshow(data, cmap='bone')
fig.colorbar(im, cax=cax, orientation='vertical')
plt.show()
The colorbar has to have its own axes. However, you can create an axes that overlaps with the previous one. Then use the cax kwarg to tell fig.colorbar to use the new axes.
For example:
import numpy as np
import matplotlib.pyplot as plt
data = np.arange(100, 0, -1).reshape(10, 10)
fig, ax = plt.subplots()
cax = fig.add_axes([0.27, 0.8, 0.5, 0.05])
im = ax.imshow(data, cmap='gist_earth')
fig.colorbar(im, cax=cax, orientation='horizontal')
plt.show()
Couldn't add this as a comment, but in case anyone is interested in using the accepted answer with subplots, the divider should be formed on specific axes object (rather than on the numpy.ndarray returned from plt.subplots)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data = np.arange(100, 0, -1).reshape(10, 10)
fig, ax = plt.subplots(ncols=2, nrows=2)
for row in ax:
for col in row:
im = col.imshow(data, cmap='bone')
divider = make_axes_locatable(col)
cax = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(im, cax=cax, orientation='vertical')
plt.show()

Plotting two images in Matplotlib on the same axes

I'm trying to plot two images on the same axes. Here is the code I'm experimenting with (I'm new to MatplotLib - so apologies in advance)...
import matplotlib.pyplot as plt
from matplotlib import transforms
img = plt.imread('image1.gif')
fig = plt.figure()
ax = fig.add_subplot(111)
rotation_in_degrees = 60
tr = transforms.Affine2D().rotate_deg(rotation_in_degrees)
ax.imshow(img)
ax.imshow(img, transform=tr)
plt.show()
There are two issues. Firstly, only the first image appears in the display; the second is only partially shown. Is there a way to show the two images in the same plot? That is, the axes are automatically scaled.
Secondly, as you can see, I'm trying to rotate the image but I'm sure that I'm not doing correctly in the sense I don't know if I'm rotating it around the origin of the plot etc. Any advice or links for other posts would be great!
Thanks for you help in advance.
To solve your firs problem - you need to display each image in separate subplot, now - you are plotting in the same subplot. See example of plotting in separate plots:
import matplotlib.pyplot as plt
from matplotlib import transforms
img = plt.imread('image.gif')
fig = plt.figure()
rotation_in_degrees = 60
tr = transforms.Affine2D().rotate_deg(rotation_in_degrees)
ax = fig.add_subplot(121)
ax.imshow(img)
ax = fig.add_subplot(122)
ax.imshow(img)
plt.show()
For the second issue with rotation - I would use PIL:
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('image.gif')
fig = plt.figure()
rotation_in_degrees = 60
ax = fig.add_subplot(121)
ax.imshow(img)
img2 = img.rotate(rotation_in_degrees)
ax = fig.add_subplot(122)
ax.imshow(img2)
plt.show()

How can I add jitter to my seaborn and matplot plots?

I am working on trying to add Jitter to my plots using seaborn and matplot plots. I am getting mixed information form what I am reading online. Some information is saying coding needs to be done and other information show it as being as simple as jitter = True. I there another library or something that I should be importing that I am not aware of? Below is the code that I am running and trying to add jitter to:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
filename = 'https://library.startlearninglabs.uw.edu/DATASCI410/Datasets/JitteredHeadCount.csv'
headcount_df = pd.read_csv(filename)
headcount_df.describe()
%matplotlib inline
ax = plt.figure(figsize=(12, 6)).gca() # define axis
headcount_df.plot.scatter(x = 'Hour', y = 'TablesOpen', ax = ax, alpha = 0.2)
# auto_price.plot(kind = 'scatter', x = 'city-mpg', y = 'price', ax = ax)
ax.set_title('Hour vs TablesOpen') # Give the plot a main title
ax.set_ylabel('TablesOpen')# Set text for y axis
ax.set_xlabel('Hour')
ax = sns.kdeplot(headcount_df.loc[:, ['TablesOpen', 'Hour']], shade = True, cmap = 'PuBu')
headcount_df.plot.scatter(x = 'Hour', y = 'TablesOpen', ax = ax, jitter = True)
ax.set_title('Hour vs TablesOpen') # Give the plot a main title
ax.set_ylabel('TablesOpen')# Set text for y axis
ax.set_xlabel('Hour')
I receive the error: AttributeError: 'PathCollection' object has no property 'jitter' when trying to add the jitter. Any help or more information on this would be much appreciated
To add jitter to a scatter plot, first get a handle to the collection that contains the scatter dots. When a scatter plot is just created on an ax, ax.collections[-1] will be the desired collection.
Calling get_offsets() on the collection gets all the xy coordinates of the dots. Add some small random number to each of them. As in this case all coordinates are integers, adding a random number between 0 and 1 spreads the dots out evenly.
In this case the number of dots is very huge. To better see where the dots are concentrated, they can be made very small (marker=',', linewidth=0, s=1,) and be very transparent (e.g.alpha=0.1).
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
filename = 'https://library.startlearninglabs.uw.edu/DATASCI410/Datasets/JitteredHeadCount.csv'
headcount_df = pd.read_csv(filename)
fig, ax = plt.subplots(figsize=(12, 6))
headcount_df.plot.scatter(x='Hour', y='TablesOpen', marker=',', linewidth=0, s=1, alpha=.1, color='crimson', ax=ax)
dots = ax.collections[-1]
offsets = dots.get_offsets()
jittered_offsets = offsets + np.random.uniform(0, 1, offsets.shape)
dots.set_offsets(jittered_offsets)
ax.set_title('Hour vs TablesOpen') # Give the plot a main title
ax.set_ylabel('TablesOpen') # Set text for y axis
ax.set_xlabel('Hour')
ax.set_xticks(range(25))
ax.autoscale(enable=True, tight=True)
plt.tight_layout()
plt.show()
As there are a huge number of points, drawing the 2D kde takes a long time. The time can be reduced by taking a random sample from the rows. Note that to draw a 2D kde, the latest versions of Seaborn want each column as a separate parameter.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
filename = 'https://library.startlearninglabs.uw.edu/DATASCI410/Datasets/JitteredHeadCount.csv'
headcount_df = pd.read_csv(filename)
fig, ax = plt.subplots(figsize=(12, 6))
N = 5000
rand_sel_df = headcount_df.iloc[np.random.choice(range(len(headcount_df)), N)]
ax = sns.kdeplot(rand_sel_df['Hour'], rand_sel_df['TablesOpen'], shade=True, cmap='PuBu', ax=ax)
ax.set_title('Hour vs TablesOpen')
ax.set_xticks(range(25))
plt.tight_layout()
plt.show()

python matplotlib gridspec, unwanted arbitrary axis labels

I have some code to plot a grid, with the data in each cell being distinct and having a very specific position. The easiest way I found to do this was to create the grid with gridspec and use it to precisely position my subplots, however I'm having a problem where the overall grid is labelled from 0 to 1 along each axis. This happens every time, even when the dimensions of the grid are changed. Obviously these numbers have no relevance to my data, and as what I am aiming to display is qualitative rather than quantitative I would like to remove all labels from this plot entirely.
Here is a link to an image with an example of my problem
And here is the MWE that I used to create that image:
import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))
# plotting
fig = plt.figure(1)
gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')
for (j, k), img in np.ndenumerate(table):
ax = fig.add_subplot(gs[x - j - 1, k])
ax.set_xticklabels('')
ax.set_yticklabels('')
plt.show()
I have not been able to find note of anything like this problem, so any help would be greatly appreciated.
If you just want to draw a grid over the plot, use this code:
import numpy as np
import matplotlib.pyplot as plt
# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))
# plotting
fig = plt.figure(1)
plt.title('Example Plot')
plt.gca().xaxis.grid(True, color='darkgrey', linestyle='-')
plt.gca().yaxis.grid(True, color='darkgrey', linestyle='-')
plt.show()
Another variant is used gridspec:
...
# hide ticks of main axes
ax0 = plt.gca()
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])
gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')
for (j, k), img in np.ndenumerate(table):
ax = fig.add_subplot(gs[x - j - 1, k])
# hide ticks of gribspec axes
ax.get_xaxis().set_ticks([])
ax.get_yaxis().set_ticks([])

matplotlib: change title and colorbar text and tick colors

I wanted to know how to change the color of the ticks in the colorbar and how to change the font color of the title and colorbar in a figure. For example, things obviously are visible in temp.png but not in temp2.png:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
fig = plt.figure()
data = np.clip(randn(250,250),-1,1)
cax = plt.imshow(data, interpolation='nearest')
plt.title('my random fig')
plt.colorbar()
# works fine
plt.savefig('temp.png')
# title and colorbar ticks and text hidden
plt.savefig('temp2.png', facecolor="black", edgecolor="none")
Thanks
Previous answer didnt give what I wanted.
This is how I did it:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
data = np.clip(randn(250,250),-1,1)
data = np.ma.masked_where(data > 0.5, data)
fig, ax1 = plt.subplots(1,1)
im = ax1.imshow(data, interpolation='nearest')
cb = plt.colorbar(im)
fg_color = 'white'
bg_color = 'black'
# IMSHOW
# set title plus title color
ax1.set_title('ax1 title', color=fg_color)
# set figure facecolor
ax1.patch.set_facecolor(bg_color)
# set tick and ticklabel color
im.axes.tick_params(color=fg_color, labelcolor=fg_color)
# set imshow outline
for spine in im.axes.spines.values():
spine.set_edgecolor(fg_color)
# COLORBAR
# set colorbar label plus label color
cb.set_label('colorbar label', color=fg_color)
# set colorbar tick color
cb.ax.yaxis.set_tick_params(color=fg_color)
# set colorbar edgecolor
cb.outline.set_edgecolor(fg_color)
# set colorbar ticklabels
plt.setp(plt.getp(cb.ax.axes, 'yticklabels'), color=fg_color)
fig.patch.set_facecolor(bg_color)
plt.tight_layout()
plt.show()
#plt.savefig('save/to/pic.png', dpi=200, facecolor=bg_color)
(Update: The information in this answer is outdated, please scroll below for other answers which is up to date and better suited to new version)
This can be done by inspecting and setting properties for object handler in matplotlib.
I edited your code and put some explanation in comment:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
fig = plt.figure()
data = np.clip(randn(250,250),-1,1)
cax = plt.imshow(data, interpolation='nearest')
title_obj = plt.title('my random fig') #get the title property handler
plt.getp(title_obj) #print out the properties of title
plt.getp(title_obj, 'text') #print out the 'text' property for title
plt.setp(title_obj, color='r') #set the color of title to red
axes_obj = plt.getp(cax,'axes') #get the axes' property handler
ytl_obj = plt.getp(axes_obj, 'yticklabels') #get the properties for
# yticklabels
plt.getp(ytl_obj) #print out a list of properties
# for yticklabels
plt.setp(ytl_obj, color="r") #set the color of yticks to red
plt.setp(plt.getp(axes_obj, 'xticklabels'), color='r') #xticklabels: same
color_bar = plt.colorbar() #this one is a little bit
cbytick_obj = plt.getp(color_bar.ax.axes, 'yticklabels') #tricky
plt.setp(cbytick_obj, color='r')
plt.savefig('temp.png')
plt.savefig('temp2.png', facecolor="black", edgecolor="none")
While the other answers are surely correct, it seems this is easier being solved using either styles or specific rcParams, or using the tick_params function
Styles
Matplotlib provides a dark_background style. You may use it e.g. via plt.style.use("dark_background"):
import matplotlib.pyplot as plt
import numpy as np
plt.style.use("dark_background")
fig = plt.figure()
data = np.clip(np.random.randn(150,150),-1,1)
plt.imshow(data)
plt.title('my random fig')
plt.colorbar()
plt.savefig('temp2.png', facecolor="black", edgecolor="none")
plt.show()
Or, if you need to create the same figure with and without black background styles may be used in a context.
import matplotlib.pyplot as plt
import numpy as np
def create_plot():
fig = plt.figure()
data = np.clip(np.random.randn(150,150),-1,1)
plt.imshow(data)
plt.title('my random fig')
plt.colorbar()
return fig
# create white background plot
create_plot()
plt.savefig('white_bg.png')
with plt.style.context("dark_background"):
create_plot()
plt.savefig('dark_bg.png', facecolor="black", edgecolor="none")
Read more about this in the Customizing matplotlib tutorial.
Specific rcParams
You may individually set the required rcParams that compose a style where needed in your script.
E.g. to make any text blue and yticks red:
params = {"text.color" : "blue",
"xtick.color" : "crimson",
"ytick.color" : "crimson"}
plt.rcParams.update(params)
This will automatically also colorize the tickmarks.
Customizing ticks and labels
You may also customize the objects in the plot individually. For ticks and ticklabels there is a tick_params method. E.g. to only make the ticks of the colorbar red,
cbar = plt.colorbar()
cbar.ax.tick_params(color="red", width=5, length=10)
Based on previous answer I added two lines to set the colorbar's box color and colorbar's ticks color:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
fig = plt.figure()
data = np.clip(randn(250,250),-1,1)
cax = plt.imshow(data, interpolation='nearest')
title_obj = plt.title('my random fig') #get the title property handler
plt.setp(title_obj, color='w') #set the color of title to white
axes_obj = plt.getp(cax,'axes') #get the axes' property handler
plt.setp(plt.getp(axes_obj, 'yticklabels'), color='w') #set yticklabels color
plt.setp(plt.getp(axes_obj, 'xticklabels'), color='w') #set xticklabels color
color_bar = plt.colorbar()
plt.setp(plt.getp(color_bar.ax.axes, 'yticklabels'), color='w') # set colorbar
# yticklabels color
##### two new lines ####
color_bar.outline.set_color('w') #set colorbar box color
color_bar.ax.yaxis.set_tick_params(color='w') #set colorbar ticks color
##### two new lines ####
plt.setp(cbytick_obj, color='r')
plt.savefig('temp.png')
plt.savefig('temp3.png', facecolor="black", edgecolor="none")
Also, you can change the tick labels with:
cax = plt.imshow(data)
cbar = plt.colorbar(orientation='horizontal', alpha=0.8, label ='my label',
fraction=0.075, pad=0.07, extend='max')
#get the ticks and transform it to list, if you want to add strings.
cbt = cbar.get_ticks().tolist()
#edit the new list of ticks, for instance the firs element
cbt[0]='$no$ $data$'
# then, apply the changes on the actual colorbar
cbar.ax.set_xticklabels(cbt)

Categories