Using a proxy artist inside a legend, matplotlib, Python - python

Here is a snippet of my code:
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
ax1.scatter((logngal),(logm200),c='r',label='$0.0<z<1.0$')
ax1.plot((logngal),(curve_y_1),'y',linewidth=2,label='$slope=%s \pm %s$'%(slope1,slope1_err))
ax1.fill_between(x_pred, lower, upper, color='#888888', alpha=0.5)
p1 = mpatches.Rectangle((0, 0), 1, 1, fc="#888888",alpha=0.5)
ax1.legend([p1],['$1\sigma\/confidence\/limts$'])
fig2.show()
When I perform the above, I only see $1\sigma\/confidence\/limts$ mentioned in the legend.
Whereas as you can see that I also call label='$0.0<z<1.0$' and label='$slope=%s \pm %s$'%(slope1,slope1_err) in ax1.scatter and ax1.plot respectively.
This does not get plotted in the legend.
How do I add all the above three labels inside the legend?

you need to grab the scatter and plot artists as you plot them, and then feed the handles and labels from them to legend. For example, here's your code modified (with some sample data at the beginning just to get it to run):
plt.plot returns a list of Line2D objects, so if you read it as pplot, = plt.plot(...), you unpack that one-item list.
You can then use .get_label() on pplot and pscat to give the labels to the legend.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
# Some things to make your script run when I don't have your data
slope1,slope1_err='this','that'
logngal = np.linspace(0,1,20)
logm200 = np.random.rand(20)
x_pred = np.linspace(0,1,20)
curve_y_1 = 0.5*(np.sin(logngal)/2.+np.cos(logngal))
upper = np.sin(x_pred)/2.
lower = np.cos(x_pred)
# end of sample data
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
pscat = ax1.scatter((logngal),(logm200), c='r',label='$0.0<z<1.0$')
pplot, = ax1.plot((logngal),(curve_y_1),'y',linewidth=2,label='$slope=%s \pm %s$'%(slope1,slope1_err))
ax1.fill_between(x_pred, lower, upper,color='#888888', alpha=0.5)
p1 = mpatches.Rectangle((0, 0), 1, 1, fc="#888888",alpha=0.5)
handles = [p1,pplot,pscat]
labels = ['$1\sigma\/confidence\/limts$',pplot.get_label(),pscat.get_label()]
ax1.legend(handles,labels)
fig2.show()

Related

Python Plots - Plotting a subplots in a subplots

I want to plot a graph representing the changes as per the varying variables. The sample figure is shown below.
The idea is to plot subplot within a subplot. Note It is different from plotting a graph using subplot with a predefined number of rows and columns, i.e matplotlib.pyplot.subplots(nrows=2, ncols=2)
Can I plot such figures using matplotlib/seaborn?
I have drawn the frames and placed the axes inside the frames, everything is based on the no. of subplots/frame, the no. of rows and columns of the frames' grid and the physical dimensions of the different elements.
I imagine that most of the code is self explanatory, except the part where we place the axes in the precise locations, that's stolen from the Demo Fixed Size Axes, if you see points in need of elucidation please ask
import matplotlib
from mpl_toolkits.axes_grid1 import Divider, Size
from mpl_toolkits.axes_grid1.mpl_axes import Axes
import matplotlib.pyplot as plt
import numpy as np
from itertools import product
mm = lambda d: d/25.4
nplots = 2
wp, hp = mm(40), mm(28)
dxp, dyp = mm(16), mm(12)
nrows, ncols = 3, 2
wf, hf = nplots*(wp+dxp), hp+dyp
dxf, dyf = mm(10), mm(8)
xcorners, ycorners = (np.arange(dxf/2,ncols*(wf+dxf),wf+dxf),
np.arange(dyf/2,nrows*(hf+dyf),hf+dyf))
# plus 10 mm for suptitle
fig = plt.figure(figsize=(ncols*(wf+dxf), nrows*(hf+dyf)+mm(10)))
rect = lambda xy: plt.Rectangle(xy, wf, hf,
transform=fig.dpi_scale_trans,
figure=fig,
edgecolor='k', facecolor='none')
fig.patches.extend([rect(xy) for xy in product(xcorners, ycorners)])
t = np.linspace(0,3.14,315); s = np.sin(t)
for nframe, (y, x) in enumerate(product(ycorners, xcorners), 1):
for n in range(nplots):
divider = Divider(fig, (0.0, 0.0, 1., 1.),
[Size.Fixed(x+0.7*dxp+n*(wp+dxp)), Size.Fixed(wp)],
[Size.Fixed(y+0.7*dyp ), Size.Fixed(hp)],
aspect=False)
ax = Axes(fig, divider.get_position())
ax.set_axes_locator(divider.new_locator(nx=1, ny=1))
ax.plot(t, s)
fig.add_axes(ax)
fig.text(x, y, 'Frame %d'%nframe, transform=fig.dpi_scale_trans)
figsize = fig.get_size_inches()
width = figsize[0]*25.4 # mm
fig.suptitle('Original figure width is %.2f mm - everything is scaled'%width)
fig.savefig('pippo.png', dpi=118, facecolor='#f8f8f0')
You will need to use Matplotlib to plot these graphs
You can follow the following example to create your own figure with the graphs:
import matplotlib.pyplot as plt
plt.subplot(1, 2, 1) # Args ( Lines, Columns, Reference )
plt.plot(x, y, 'r') # Reference will say what graph we are modding
plt.subplot(1, 2, 2)
plt.plot(y, x, 'g')
plt.show()
The code will create one graph like this:
And you can use plt.xlabel('name'), plt.ylabel('name') and plt.title('name') to define the labels and the title of your figure
Note: The code above will create one image with 2 graphs, and you can use this code inside another block of code to create the image that you want.
You can also use the following code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=5, ncols=5, figsize=(5, 5))
ax[0, 0].plot(x, y) # The method ax is now one array and is referred by indexes
ax[0, 0].set_title('Title')
ax[1, 1].plot(x, y)
ax[1, 1].set_title('Title')
plt.tight_layout() # It will separate the graphs to avoid overlays
plt.show()
It will create the following image:

matplotlib legend label under key?

How to adjust label location relate to key?I reclassified the data and displayed a discrete corbar which looks like multi-handles legend. Actually ,I couldn't find any parameters about the location of labels(text or numbers).The default setting is keys in left while label in right. Could I change the position? such as labels under keys or above. My purpose is to show the legend as follows (label under key and no space between keys:
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randint(8, size=(100,100))
cmap = plt.cm.get_cmap('PiYG', 8)
plt.pcolormesh(data,cmap = cmap,alpha = 0.75)
# Set borders in the interval [0, 1]
bound = np.linspace(0, 1, 9)
# Preparing borders for the legend
bound_prep = np.round(bound * 7, 2)
# Creating 8 Patch instances
plt.legend([mpatches.Patch(color=cmap(b)) for b in bound[:-1]],
['{}'.format(bound_prep[i]) for i in range(8)],
bbox_to_anchor=(0,-0.25,1,0.2),ncol=len(bound))
It seems that there is no parameters to adjust location of labels.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randint(8, size=(100,100))
cmap = plt.cm.get_cmap('PiYG', 8)
fig, ax = plt.subplots()
pcm = ax.pcolormesh(data,cmap = cmap,alpha = 0.75, vmin=0, vmax=8)
fig.colorbar(pcm, ax=ax)
plt.show()

Animate a annotation from a list of XY Coordinates

I am trying to animate annotations from a list of xy coordinates. The code below animates the annotation line but I cannot get the arrow function to animate using the same code.
The example dataset is a representation of the data I'm using. It is horizontally formatted. With this, I make a list from all the X-Coordinates and all the Y-Coordiantes from each subject. I then make a list pertaining to each time point, which is each row of data. From that I can plot a scatter and annotations.
However, when trying to plot an arrow between two separate coordinates I run into the error as stated by #ImportanceOfBeingErnest. The function should be a tuple of two elements but I'm having trouble with the arrow animation function as I think I need to provide 4 elements. The X and Y coordinate for the first point and X and Y coordinate for the second point.
Will I have to re-format that data or is there a way to animate the arrow function were 4 tuples are required?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x_data = np.random.randint(80, size=(400, 4))
y_data = np.random.randint(80, size=(400, 4))
lists = [[],[]]
lists[0] = x_data
lists[1] = y_data
fig, ax = plt.subplots(figsize = (8,6))
ax.set_xlim(0,80)
ax.set_ylim(0,80)
scatter = ax.scatter(lists[0][0], lists[1][0], zorder = 5) #Scatter plot
annotation = ax.annotate(' Subject 1', xy=(lists[0][0][2],lists[1][0][2]))
arrow = ax.annotate('', xy = (lists[0][0][0], lists[1][0][0]), xytext = (lists[0][0][1],lists[1][0][1]), arrowprops = {'arrowstyle': "<->"})
def animate(i) :
scatter.set_offsets([[[[[lists[0][0+i][0], lists[1][0+i][0]], [lists[0][0+i][1], lists[1][0+i][1]], [lists[0][0+i][2], lists[1][0+i][2]], [lists[0][0+i][3], lists[1][0+i][3]]]]]])
Subject_x = lists[0][0+i][2]
Subject_y = lists[1][0+i][2]
annotation.set_position((Subject_x, Subject_y))
annotation.xy = (Subject_x, Subject_y)
Arrow1 = (lists[0][0+i][0], lists[1][0+i][0]) #Code for arrow animation doesn't work. Produces a blank screen after the 1st frame
Arrow2 = (lists[0][0+i][1], lists[1][0+i][1])
arrow.set_position((Arrow1, Arrow2))
arrow.xy = (Arrow1, Arrow2)
ani = animation.FuncAnimation(fig, animate,
interval = 50, blit = False)
plt.show()
The solution to this is still given in this question:
Animate points with labels with mathplotlib
As said several times in the comments, each position is determined by a tuple of two values (x,y). Hence you cannot provide a tuple of tuples to those positions. Also the start and end of the arrow should of course be at different positions.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x_data = np.random.randint(80, size=(400, 4))
y_data = np.random.randint(80, size=(400, 4))
fig, ax = plt.subplots(figsize = (8,6))
ax.set_xlim(0,80)
ax.set_ylim(0,80)
scatter = ax.scatter(x_data[0], y_data[0], zorder = 5) #Scatter plot
annotation = ax.annotate(' Subject 1', xy=(x_data[0,2],y_data[0,2]))
arrow = ax.annotate('', xy = (x_data[0,0], y_data[0,0]),
xytext = (x_data[0,1],y_data[0,1]),
arrowprops = {'arrowstyle': "<->"})
def animate(i) :
scatter.set_offsets(np.c_[x_data[i,:], y_data[i,:]])
annotation.set_position((x_data[i,2], y_data[i,2]))
start = (x_data[i,0], y_data[i,0])
end = (x_data[i,1], y_data[i,1])
arrow.set_position(start)
arrow.xy = end
ani = animation.FuncAnimation(fig, animate, frames=len(x_data),
interval = 700, blit = False)
plt.show()

setting margins in matplotlib/seaborn with subplots

i'm plotting subplots in matplotlib/seaborn using:
plt.figure()
s1 = plt.subplot(2, 1, 1)
# plot 1
# call seaborn here
s2 = plt.subplot(2, 1, 2)
# plot 2
plt.tight_layout()
plt.show()
i am running into the common issue of marker being hidden by the axis (Add margin when plots run against the edge of the graph). when i try to adjust margins it doesn't work:
s1 = plt.subplot(2, 1, 1)
s1.margins(0.05)
it gives no error but doesn't set margins either.
here is a complete example:
gammas = sns.load_dataset("gammas")
s = plt.subplot(1, 1, 1)
# this does not change the x margins
s.get_axes().margins(x=0.05, y=0.01)
ax = sns.tsplot(time="timepoint", value="BOLD signal",
unit="subject", condition="ROI",
err_style="ci_bars",
interpolate=False,
data=gammas)
plt.show()
in the above, i am trying to make the x-margins bigger, but the x argument to margins() seems to have no effect. how can this be done?
You can define a function to add a given fraction of the x and y ranges to the margin, which makes use of get_xlim, get_ylim, set_xlim and set_ylim. Using your minimal example:
import matplotlib.pyplot as plt
import seaborn as sns
def add_margin(ax,x=0.05,y=0.05):
# This will, by default, add 5% to the x and y margins. You
# can customise this using the x and y arguments when you call it.
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xmargin = (xlim[1]-xlim[0])*x
ymargin = (ylim[1]-ylim[0])*y
ax.set_xlim(xlim[0]-xmargin,xlim[1]+xmargin)
ax.set_ylim(ylim[0]-ymargin,ylim[1]+ymargin)
gammas = sns.load_dataset("gammas")
s = plt.subplot(1, 1, 1)
ax = sns.tsplot(time="timepoint", value="BOLD signal",
unit="subject", condition="ROI",
err_style="ci_bars",
interpolate=False,
data=gammas)
# Check what the original limits were
x0,y0=s.get_xlim(),s.get_ylim()
# Update the limits using set_xlim and set_ylim
add_margin(s,x=0.05,y=0.01) ### Call this after tsplot
# Check the new limits
x1,y1=s.get_xlim(),s.get_ylim()
# Print the old and new limits
print x0,y0
print x1,y1
plt.show()
Which prints:
# The original limits
(-0.10101010101010099, 10.1010101010101) (-2.0, 3.0)
# The updated limits
(-0.61111111111111105, 10.611111111111111) (-2.0499999999999998, 3.0499999999999998)
And here's the figure this produces:
Which, when compared to the original figure, clearly has added margins:

Plot representative errorbar size in legend?

I have some data (with errors) to be plotted in a rather dense display. I would like to plot these points without errorbars (because it makes it too busy), but to plot a representative error bar in a legend (which shows the errorbar with an accurate size).
Here is what I have so far (which is not successful).
import pylab as pl
p1, = pl.plot([1,2,3], label="test1")
p2, = pl.plot([3,2,1], label="test2")
errorbarsize = 1.65 # Need this to be properly scaled in the legend
# p3, = pl.plot([1], label='data', color="red") # works
# p3, = pl.scatter(1, 1, label='data', color="red")
# p3, = pl.errorbar(1, 1, yerr=errorbarsize, label='data', color="red")
l1 = pl.legend([p1], ["Label 1"], loc=1)
l2 = pl.legend([p2], ["Label 2"], loc=2) # this removes l1 from the axes.
l3 = pl.legend([p3], ["Label 3"], loc=3, numpoints=1)
gca().add_artist(l1) # add l1 as a separate artist to the axes
gca().add_artist(l2) # add l2 as a separate artist to the axes
Also, it would be best if I could plot this in a separate legend, but that might be asking too much.
Here is an example using Thorsten Kranz's suggestion, and another example of how to show representative error bars...
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2,1)
# -- make some fake data
x = np.random.normal(loc=4, size=100)
x.sort()
y = np.random.normal(loc=5, size=100)
y.sort()
y_errorbarsize = y.std()
x_errorbarsize = y.std()
### Example 1
# From Thorsten Kranz comment...
axs[0].plot(x, y, label="Example #1")
axs[0].fill_between(x,y-y_errorbarsize/2, y+y_errorbarsize/2,alpha=0.3)
### Example 2
axs[1].scatter(x, y, label="Example #2", alpha=0.8)
# Place our representative error bar by hand.
axis_coordinates_of_representative_error_bar = (0.1, 0.8)
screen_coordinates_of_representative_error_bar = axs[1].transAxes.transform(axis_coordinates_of_representative_error_bar)
screen_to_data_transform = axs[1].transData.inverted().transform
data_coordinates_of_representative_error_bar = screen_to_data_transform(screen_coordinates_of_representative_error_bar)
foo = data_coordinates_of_representative_error_bar
axs[1].errorbar(foo[0], foo[1], xerr=x_errorbarsize/2, yerr=y_errorbarsize/2, capsize=3)
plt.show()
plt.close()

Categories

Resources