using PyPlot in Julia 1.7: colorbar & equal 3D axis - matplotlib

I'm updating the syntax for julia 1.7. I have everything below working for julia 0.x, and below represents all I found for 1.7, but there are still glitches.
Question 1. How do I get the colorbar to match the colors on the torus? (And why is the cmap = "hot" not working?)
Question 2. Aspect "auto" works, but "equal" gives error "Axes3D currently only supports the aspect argument 'auto'", so how do I get axis equal aspect ratio?
using MAT
using PyPlot
using3D()
N = 200;
R = 10.0;
r = 0.3*R;
dx = 2*pi/(N);
y = zeros(N,1);
x = transpose(y) .+ 0.0;
for ix = 2:N; y[ix] = (ix-1)*dx; x[ix] = (ix-1)*dx; end
x = x .- pi;
y = y .- pi;
cosxsqr = cos.(x) .+ 0.0*y;
sinxsqr = sin.(x) .+ 0.0*y;
sinysqr = 0.0*x .+ sin.(y);
cosysqr = 0.0*x .+ cos.(y);
Rrcosxsqr = broadcast(+,r*cosxsqr,R);
rRrcosx = r*Rrcosxsqr[:];
Xsqr = Rrcosxsqr.*cosysqr;
Ysqr = Rrcosxsqr.*sinysqr;
Zsqr = r*sinxsqr;
display("maxZ")
display(maximum(Zsqr[:]))
display("minZ")
display(minimum(Zsqr[:]))
colors1 = (Zsqr.+abs(minimum(Zsqr[:])))./(2*maximum(Zsqr[:]))
display("min-color")
display(minimum(colors1[:]))
display("max-color")
display(maximum(colors1[:]))
colors3 = cat(colors1,colors1,colors1,dims=3)
pmesh = pcolormesh(colors1)
figure(1)
ax = subplot(1,1,1, projection="3d");
surf(Xsqr,Ysqr,Zsqr,facecolors=colors3,vmin=minimum(abs.(Zsqr[:])),vmax=maximum(abs.(Zsqr[:])),cmap="hot") # Q1 how to get correct colorbar
ax.set_aspect("auto") # Q2 how to get aspect equal
colorbar(pmesh)
Here is resulting pic.

Making my comment an answer since it became too long.
Some of Matplotlib's 3D plotting options aren't implemented as well as their 2D counterparts, but as a workaround you can change the aspect ratio of the containing box using ax.set_box_aspect((10, 10, 3)).
As for the colormap I'm not sure what you're after, your colorbar is referring to a plot that's not shown here. If you want it to be hot you can do the following:
figure(1)
ax = subplot(1,1,1, projection="3d");
s = surf(Xsqr,Ysqr,Zsqr,facecolors=colors3,vmin=minimum(abs.(Zsqr[:])),vmax=maximum(abs.(Zsqr[:])),cmap="hot")
ax.set_box_aspect((10, 10, 3))
colorbar(s, pad = 0.1)
Since you actually specify your own facecolors for the surf plot this doesn't really do anything, though. If you take out that named argument you get a 3D plot with the hot colormap:
figure(1)
ax = subplot(1,1,1, projection="3d");
s = surf(Xsqr,Ysqr,Zsqr,vmin=minimum(abs.(Zsqr[:])),vmax=maximum(abs.(Zsqr[:])),cmap="hot")
ax.set_box_aspect((10, 10, 3))
colorbar(s, pad = 0.1)
If you want your colorbar to have the same custom grayscale as your plot you need to create your own colormap (which is described well in the Matplotlib documentation)

Related

Changing the shape of a scatter plot to a line in a Julia PyPlot legend

I'm trying to change the label of a scatter plot to show a line instead of a small dot, as the dot is difficult to see on screen, let alone in print:
Does the PyPlot library allow for this? The relevant parts of my code are as follows:
println("Importing (and possibly compiling JIT) a few relevant libraries...")
using LaTeXStrings,PyPlot;
println("Calculating a few points...")
samples = 10000;
T = 2 * pi;
x = collect(range(-pi,stop=pi,length=samples));
stepf = sign.(x);
N = 40;
"""
Sums of f
"""
fig, ax = subplots();
figname = "./Plots/filters.pdf";
ax[:scatter](x,stepf,label=latexstring("f(t)"),s=1);
ax[:axis]("off");
ax[:set_xlabel](latexstring("t"));
ax[:legend](loc="lower right");
fig[:savefig](figname);
close(fig)
EDIT
Based on below comments, this is going to boil down to finding a way to access Matplotlib's Line2D objects through Julia.
Here is the code by #ImportanceOfBeingErnest translated from Python to Julia PyCall. Definitely works!
h,l = ax[:get_legend_handles_labels]()
z = PyPlot.plt[:Line2D]([],[], color="C0")
h[end] = z
ax[:legend](labels=l, handles=h, loc="lower right");

Marker size/alpha scaling with window size/zoom in plot/scatter

When exploring data sets with many points on an xy chart, I can adjust the alpha and/or marker size to give a good quick visual impression of where the points are most densely clustered. However when I zoom in or make the window bigger, the a different alpha and/or marker size is needed to give the same visual impression.
How can I have the alpha value and/or the marker size increase when I make the window bigger or zoom in on the data? I am thinking that if I double the window area I could double the marker size, and/or take the square root of the alpha; and the opposite for zooming.
Note that all points have the same size and alpha. Ideally the solution would work with plot(), but if it can only be done with scatter() that would be helpful also.
You can achieve what you want with matplotlib event handling. You have to catch zoom and resize events separately. It's a bit tricky to account for both at the same time, but not impossible. Below is an example with two subplots, a line plot on the left and a scatter plot on the right. Both zooming (factor) and resizing of the figure (fig_factor) re-scale the points according to the scaling factors in figure size and x- and y- limits. As there are two limits defined -- one for the x and one for the y direction, I used here the respective minima for the two factors. If you'd rather want to scale with the larger factors, change the min to max in both event functions.
from matplotlib import pyplot as plt
import numpy as np
fig, axes = plt.subplots(nrows=1, ncols = 2)
ax1,ax2 = axes
fig_width = fig.get_figwidth()
fig_height = fig.get_figheight()
fig_factor = 1.0
##saving some values
xlim = dict()
ylim = dict()
lines = dict()
line_sizes = dict()
paths = dict()
point_sizes = dict()
## a line plot
x1 = np.linspace(0,np.pi,30)
y1 = np.sin(x1)
lines[ax1] = ax1.plot(x1, y1, 'ro', markersize = 3, alpha = 0.8)
xlim[ax1] = ax1.get_xlim()
ylim[ax1] = ax1.get_ylim()
line_sizes[ax1] = [line.get_markersize() for line in lines[ax1]]
## a scatter plot
x2 = np.random.normal(1,1,30)
y2 = np.random.normal(1,1,30)
paths[ax2] = ax2.scatter(x2,y2, c = 'b', s = 20, alpha = 0.6)
point_sizes[ax2] = paths[ax2].get_sizes()
xlim[ax2] = ax2.get_xlim()
ylim[ax2] = ax2.get_ylim()
def on_resize(event):
global fig_factor
w = fig.get_figwidth()
h = fig.get_figheight()
fig_factor = min(w/fig_width,h/fig_height)
for ax in axes:
lim_change(ax)
def lim_change(ax):
lx = ax.get_xlim()
ly = ax.get_ylim()
factor = min(
(xlim[ax][1]-xlim[ax][0])/(lx[1]-lx[0]),
(ylim[ax][1]-ylim[ax][0])/(ly[1]-ly[0])
)
try:
for line,size in zip(lines[ax],line_sizes[ax]):
line.set_markersize(size*factor*fig_factor)
except KeyError:
pass
try:
paths[ax].set_sizes([s*factor*fig_factor for s in point_sizes[ax]])
except KeyError:
pass
fig.canvas.mpl_connect('resize_event', on_resize)
for ax in axes:
ax.callbacks.connect('xlim_changed', lim_change)
ax.callbacks.connect('ylim_changed', lim_change)
plt.show()
The code has been tested in Pyton 2.7 and 3.6 with matplotlib 2.1.1.
EDIT
Motivated by the comments below and this answer, I created another solution. The main idea here is to only use one type of event, namely draw_event. At first the plots did not update correctly upon zooming. Also ax.draw_artist() followed by a fig.canvas.draw_idle() like in the linked answer did not really solve the problem (however, this might be platform/backend specific). Instead I added an extra call to fig.canvas.draw() whenever the scaling changes (the if statement prevents infinite loops).
In addition, do avoid all the global variables, I wrapped everything into a class called MarkerUpdater. Each Axes instance can be registered separately to the MarkerUpdater instance, so you could also have several subplots in one figure, of which some are updated and some not. I also fixed another bug, where the points in the scatter plot scaled wrongly -- they should scale quadratic, not linear (see here).
Finally, as it was missing from the previous solution, I also added updating for the alpha value of the markers. This is not quite as straight forward as the marker size, because alpha values must not be larger than 1.0. For this reason, in my implementation the alpha value can only be decreased from the original value. Here I implemented it such that the alpha decreases when the figure size is decreased. Note that if no alpha value is provided to the plot command, the artist stores None as alpha value. In this case the automatic alpha tuning is off.
What should be updated in which Axes can be defined with the features keyword -- see below if __name__ == '__main__': for an example how to use MarkerUpdater.
EDIT 2
As pointed out by #ImportanceOfBeingErnest, there was a problem with infinite recursion with my answer when using the TkAgg backend, and apparently problems with the figure not refreshing properly upon zooming (which I couldn't verify, so probably that was implementation dependent). Removing the fig.canvas.draw() and adding ax.draw_artist(ax) within the loop over the Axes instances instead fixed this issue.
EDIT 3
I updated the code to fix an ongoing issue where figure is not updated properly upon a draw_event. The fix was taken from this answer, but modified to also work for several figures.
In terms of an explanation of how the factors are obtained, the MarkerUpdater instance contains a dict that stores for each Axes instance the figure dimensions and the limits of the axes at the time it is added with add_ax. Upon a draw_event, which is for instance triggered when the figure is resized or the user zooms in on the data, the new (current) values for figure size and axes limits are retrieved and a scaling factor is calculated (and stored) such that zooming in and increasing the figure size makes the markers bigger. Because x- and y-dimensions may change at different rates, I use min to pick one of the two calculated factors and always scale against the original size of the figure.
If you want your alpha to scale with a different function, you can easily change the lines that adjust the alpha value. For instance, if you want a power law instead of a linear decrease, you can write path.set_alpha(alpha*facA**n), where n is the power.
from matplotlib import pyplot as plt
import numpy as np
##plt.switch_backend('TkAgg')
class MarkerUpdater:
def __init__(self):
##for storing information about Figures and Axes
self.figs = {}
##for storing timers
self.timer_dict = {}
def add_ax(self, ax, features=[]):
ax_dict = self.figs.setdefault(ax.figure,dict())
ax_dict[ax] = {
'xlim' : ax.get_xlim(),
'ylim' : ax.get_ylim(),
'figw' : ax.figure.get_figwidth(),
'figh' : ax.figure.get_figheight(),
'scale_s' : 1.0,
'scale_a' : 1.0,
'features' : [features] if isinstance(features,str) else features,
}
ax.figure.canvas.mpl_connect('draw_event', self.update_axes)
def update_axes(self, event):
for fig,axes in self.figs.items():
if fig is event.canvas.figure:
for ax, args in axes.items():
##make sure the figure is re-drawn
update = True
fw = fig.get_figwidth()
fh = fig.get_figheight()
fac1 = min(fw/args['figw'], fh/args['figh'])
xl = ax.get_xlim()
yl = ax.get_ylim()
fac2 = min(
abs(args['xlim'][1]-args['xlim'][0])/abs(xl[1]-xl[0]),
abs(args['ylim'][1]-args['ylim'][0])/abs(yl[1]-yl[0])
)
##factor for marker size
facS = (fac1*fac2)/args['scale_s']
##factor for alpha -- limited to values smaller 1.0
facA = min(1.0,fac1*fac2)/args['scale_a']
##updating the artists
if facS != 1.0:
for line in ax.lines:
if 'size' in args['features']:
line.set_markersize(line.get_markersize()*facS)
if 'alpha' in args['features']:
alpha = line.get_alpha()
if alpha is not None:
line.set_alpha(alpha*facA)
for path in ax.collections:
if 'size' in args['features']:
path.set_sizes([s*facS**2 for s in path.get_sizes()])
if 'alpha' in args['features']:
alpha = path.get_alpha()
if alpha is not None:
path.set_alpha(alpha*facA)
args['scale_s'] *= facS
args['scale_a'] *= facA
self._redraw_later(fig)
def _redraw_later(self, fig):
timer = fig.canvas.new_timer(interval=10)
timer.single_shot = True
timer.add_callback(lambda : fig.canvas.draw_idle())
timer.start()
##stopping previous timer
if fig in self.timer_dict:
self.timer_dict[fig].stop()
##storing a reference to prevent garbage collection
self.timer_dict[fig] = timer
if __name__ == '__main__':
my_updater = MarkerUpdater()
##setting up the figure
fig, axes = plt.subplots(nrows = 2, ncols =2)#, figsize=(1,1))
ax1,ax2,ax3,ax4 = axes.flatten()
## a line plot
x1 = np.linspace(0,np.pi,30)
y1 = np.sin(x1)
ax1.plot(x1, y1, 'ro', markersize = 10, alpha = 0.8)
ax3.plot(x1, y1, 'ro', markersize = 10, alpha = 1)
## a scatter plot
x2 = np.random.normal(1,1,30)
y2 = np.random.normal(1,1,30)
ax2.scatter(x2,y2, c = 'b', s = 100, alpha = 0.6)
## scatter and line plot
ax4.scatter(x2,y2, c = 'b', s = 100, alpha = 0.6)
ax4.plot([0,0.5,1],[0,0.5,1],'ro', markersize = 10) ##note: no alpha value!
##setting up the updater
my_updater.add_ax(ax1, ['size']) ##line plot, only marker size
my_updater.add_ax(ax2, ['size']) ##scatter plot, only marker size
my_updater.add_ax(ax3, ['alpha']) ##line plot, only alpha
my_updater.add_ax(ax4, ['size', 'alpha']) ##scatter plot, marker size and alpha
plt.show()

How to add patches to a figure using Julia's PyPlot.jl

Example:
using PyPlot
fig = gcf(); ax0=subplot(2,2,2)
ax1 = subplot(2,2,4)
ax0tr = ax0[:transAxes]; ax1tr = ax1[:transAxes]
figtr = fig[:transFigure]
# 2. Transform arroww start point from axis 0 to figure coordinates
ptB = figtr[:transform](ax0tr[:transform]((20., -0.5)))
# 3. Transform arroww end point from axis 1 to figure coordinates
ptE = figtr[:transform](ax1tr[:transform]((20., 1.)))
# 4. Create the patch
arroww = matplotlib[:patches][:FancyArrowPatch](
ptB, ptE, transform=figtr, # Place arroww in figure coord system
fc = "C0", alpha = 0.25, connectionstyle="arc3,rad=0.2",
arrowstyle="simple",
mutation_scale = 40.0)
# 5. Add patch to list of objects to draw onto the figure
push!(fig[:patches], arroww)
fig[:show]()
How can I add a patch object to a figure and actually see it on the figure? This doesn't work. It doesn't throw any errors but I cannot see any arrows.
(I also cannot use the function arrow because I want to create an arrow that goes from subplot to subplot).
Because this is still a top search result, here is a solution (using Julia 1.5.3)
import PyPlot; const plt = PyPlot
fig = plt.figure(figsize=(6,8), dpi=150)
ax0 = fig.add_subplot(2,2,2)
ax1 = fig.add_subplot(2,2,4)
arrow = patches.ConnectionPatch(
[0.2,1],
[0.6,0.5],
coordsA=ax0.transData,
coordsB=ax1.transData,
color="black",
arrowstyle="-|>",
mutation_scale=30,
linewidth=3,
)
fig.patches = [arrow]
It produces the following plot.

Fake-rivalry stimulus

for a binocular rivalry experiment using color blobs (created with GratingStim using a gaussian mask), I need to draw a fake rivalry stimulus. That is, I need a round color blob that has one color for example on the top (25% of the color blob) and another color below (75% of the color blob). Additionally, I would like the twocolored fake rivalry blob to have a gaussian mask as my real rivalry stimuli do. Also it would be good to have a fuzzy color transition in the fake rivalry stimulus. I hope it's clear what I mean.
One solution I thought of was to draw two rectangles with blurred edges and then lay a gaussian alpha mask over them. In order to get the color proportions right, I would only have to move the two rectangles behind the mask. Is there a way to put a alpha-maks over an entire window?
Another solution would be to use ShapeStim as is suggested in this post explaining how to draw a semi circle : https://groups.google.com/forum/#!msg/psychopy-users/L9TYIrf9eJk/m0zIj0N23bMJ I would have to play around with the vertices, but I think it should work. The only thing that worries me here is that ShapeStim has no mask attribute to blur the edges.
Can you think of a way to do it?
Thank you very much!
Lilla
System specifications:
Psychopy v1.83.01 running on iOS 10.11.1
second update, even nicer result:
# Set up stimuli
from psychopy import visual, event
import numpy as np
from scipy.stats import gaussian_kde
win = visual.Window([500,500])
win2 = visual.Window([500,500])
#magic numpy stuff /scipy stuff, adapted from http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.gaussian_kde.html
mean1 = [0, 0]
#the smaller the value, the bigger the visible blob
cov1 = [[0.03, 0], [0, 0.09]] #in this mask, it should be 50/50
cov2 = [[0.05,0],[0,0.4]] #in this mask, the color with this mask is the smaller one
m1, m2 = np.random.multivariate_normal(mean1, cov1, 2000).T# * np.random.multivariate_normal(mean2, cov2, 5000).T
for i in xrange(len(m2)):
if m2[i] >= 0:
m2[i] = m2[i]* 0.5#np.random.multivariate_normal(mean2, cov2,1).T[0]
values = np.vstack([m1, m2])
kernel = gaussian_kde(values)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()
X, Y = np.mgrid[xmin:xmax:128j, ymin:ymax:128j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape) #this array will be the mask
Z = Z - 1
for i in xrange(128):
for j in xrange(128): #it will neverbe smaller than -1
if Z[i][j] > 1:
Z[i][j] = 1
# Draw them on top of each other
perc75 = visual.GratingStim(win, sf=0, size=250, color='green',pos=(0.0, 0.0), mask =Z)
perc25 = visual.GratingStim(win, sf=0, size=250, color='red',pos=(0.0, 0.0), mask = 'raisedCos', maskParams={'fringeWidth':0.8})
perc25.setAutoDraw(True)
perc75.setAutoDraw(True)
win.flip()
event.waitKeys()
Would this work? You can then locate the blobs where you want, e.g. for one eye. No need to do whole-window stuff.
# Set up stimuli
from psychopy import visual, event
win = visual.Window([500,500])
blob_large = visual.GratingStim(win, sf=0, mask='gauss', size=1, color='red')
blob_small = visual.GratingStim(win, sf=0, mask='gauss', size=0.5, color='green', maskParams={'sd':6})
# Draw them on top of each other
blob_large.draw()
blob_small.draw()
win.flip()
# Wait for keyboard before quitting.
event.waitKeys()
Update on my question: the following exemplar code solves the problem:
# -*- coding: utf-8 -*-
# adapted from https://groups.google.com/forum/#!msg/psychopy-users/69p-aAWiDGI/e4iT43cHDeEJ
from psychopy import visual, event
win = visual.Window([500,500])
#stimuli
perc25 = visual.GratingStim(win, sf=0, size=1, color='RED',pos=(0.0, 0.0), mask = 'raisedCos', maskParams={'fringeWidth':0.8})
perc75 = visual.GratingStim(win, sf=0, size=0.8, color='green',pos=(0.0, -0.15), mask = 'raisedCos', maskParams={'fringeWidth':0.6})
#prepare for the screenshot
Stimlist = [perc25, perc75]
delta = .5# larger is bigger, slower
dx = delta * win.size[1]/win.size[0]
dy = delta
rect = (-dx, +dy, +dx, -dy)#size of the screenshot
screenshot = visual.BufferImageStim(win, stim=Stimlist,rect = rect, mask = 'gauss', pos=(0.0, 0.0)) # mask can also be 'raisedCos' with a smaller delta, for exmple .2
screenshot.draw()
win.flip()
event.waitKeys()

How can draw a line in matplotlib so that the edge (not the center) of the drawn line follows the plotted data?

I'm working on a figure to show traffic levels on a highway map. The idea is that for each
highway segment, I would plot two lines - one for direction. The thickness of each
line
would correspond to the traffic volume in that direction. I need to plot the lines
so that the left edge (relative to driving direction) of the drawn line follows
the shape of the highway segment. I would like to specify the shape in data coordinates,
but I would like to specify the thickness of the line in points.
My data is like this:
[[((5,10),(-7,2),(8,9)),(210,320)],
[((8,4),(9,1),(8,1),(11,4)),(2000,1900)],
[((12,14),(17,14)),(550,650)]]
where, for example, ((5,10),(-7,2),(8,9)) is a sequence of x,y values giving the shape of a highway segment, and (210,320) is traffic volumes in the forward and reverse direction, respectively
Looks matter: the result should be pretty.
I figured out a solution using matplotlib.transforms.Transform and shapely.geometry.LineString.parallel_offset.
Note that shapely's parallel_offset method can sometimes return a MultiLineString, which
is not handled by this code. I've changed the second shape so it does not cross over itself to avoid this problem. I think this problem would happen rarely happen in my application.
Another note: the documentation for matplotlib.transforms.Transform seems to imply that the
array returned by the transform method must be the same shape as the array passed
as an argument, but adding additional points to plot in the transform method seems
to work here.
#matplotlib version 1.1.0
#shapely version 1.2.14
#Python 2.7.3
import matplotlib.pyplot as plt
import shapely.geometry
import numpy
import matplotlib.transforms
def get_my_transform(offset_points, fig):
offset_inches = offset_points / 72.0
offset_dots = offset_inches * fig.dpi
class my_transform(matplotlib.transforms.Transform):
input_dims = 2
output_dims = 2
is_separable = False
has_inverse = False
def transform(self, values):
l = shapely.geometry.LineString(values)
l = l.parallel_offset(offset_dots,'right')
return numpy.array(l.xy).T
return my_transform()
def plot_to_right(ax, x,y,linewidth, **args):
t = ax.transData + get_my_transform(linewidth/2.0,ax.figure)
ax.plot(x,y, transform = t,
linewidth = linewidth,
solid_capstyle = 'butt',
**args)
data = [[((5,10),(-7,2),(8,9)),(210,320)],
[((8,4),(9,1),(8,1),(1,4)),(2000,1900)],
[((12,14),(17,16)),(550,650)]]
fig = plt.figure()
ax = fig.add_subplot(111)
for shape, volumes in data:
x,y = zip(*shape)
plot_to_right(ax, x,y, volumes[0]/100., c = 'blue')
plot_to_right(ax, x[-1::-1],y[-1::-1], volumes[1]/100., c = 'green')
ax.plot(x,y, c = 'grey', linewidth = 1)
plt.show()
plt.close()