Matplotlib: determining when mouse is in axis text rectangle - python

So it's somewhat well known that in matplotlib zoom, pressing 'x' or 'y' when zooming will zoom on only the x or y axis. I would like to modify this slightly by subclassing the NavigationToolbar2 in backend_bases.py
Specifically, I would like to have the functionality that if the mouse is in the region on the canvas below a plot (or above, depending on where I have put my axes), or to the left or right of the plot, to choose to zoom with _zoom_mode = 'x' or 'y'. (In addition to keeping the default zoom to rect functionality when the mouse is inside the plot.)
Looking at backend_bases, it appears the method I need to modify is press_zoom
def press_zoom(self, event):
if event.button=1:
self._button_pressed=1
elif event.button == 3:
self._button_pressed=3
else:
self._button_pressed=None
return
x, y = event.x, event.y
# push the current view to define home if stack is empty
if self._views.empty(): self.push_current()
self._xypress=[]
for i, a in enumerate(self.canvas.figure.get_axes()):
if (x is not None and y is not None and a.in_axes(event) and
a.get_navigate() and a.can_zoom()) :
self._xypress.append(( x, y, a, i, a.viewLim.frozen(),
a.transData.frozen() ))
id1 = self.canvas.mpl_connect('motion_notify_event', self.drag_zoom)
id2 = self.canvas.mpl_connect('key_press_event',
self._switch_on_zoom_mode)
id3 = self.canvas.mpl_connect('key_release_event',
self._switch_off_zoom_mode)
self._ids_zoom = id1, id2, id3
self._zoom_mode = event.key
self.press(event)
and add an elif to the big if statement there and use it to set the zoom mode there, but what I cannot figure out (yet), is how to tell if the mouse is in the region appropriate for 'x' or 'y' zoom mode.
So, how can I tell when the mouse is just outside of a plot?

By converting the x and y coordinate into Axes coordinates. You can tell if they're just outside the axes, if they're less than 0 or larger than 1.
Here is a simple example of how it could work.
def is_just_outside(fig, event):
x,y = event.x, event.y
print x, y
for ax in fig.axes:
xAxes, yAxes = ax.transAxes.inverted().transform([x, y])
print xAxes, yAxes
if (-0.02 < xAxes < 0) | (1 < xAxes < 1.02):
print "just outside x-axis"
if (-0.02 < yAxes < 0) | (1 < yAxes < 1.02):
print "just outside y-axis"
x = np.linspace(-np.pi,np.pi,100)
y = np.sin(x)
fig = plt.figure()
plt.plot(x,y)
ax = fig.add_subplot(111)
fig.canvas.mpl_connect('button_press_event', lambda e: is_just_outside(fig, e))
plt.show()

Related

Pyplot: Control texts clip in one direction

I am trying to change the plot window extents on an annotated plot to "zoom" into a certain window of interest. My text annotations fall outside of the plot window. If I use clip_on = True then all the text is hidden, but I just want to trim the text outside of the x-axis.
import matplotlib.pyplot as plt
x = [0,2,4,6,8,10]
y = [3,2,5,9,6,7]
plt.plot(x,y)
for i in range(len(x)):
plt.text(x[i],11, '%d' %y[i])
plt.axis([0,5,0,10])
Full data:
Reduced window:
Desired output:
This isn't fancy at all, but it works for x_max 1 to 10:
import matplotlib.pyplot as plt
x = [0,2,4,6,8,10]
y = [3,2,5,9,6,7]
x_max = 5
plot_axis = [0,x_max,0,10]
if plot_axis[1] == x[-1:][0]:
range_set = range(len(x))
else:
try:
x_idx = x.index(plot_axis[1])
except:
x_idx = x.index(plot_axis[1]-1)
range_set = range(x_idx+1)
plt.plot(x,y)
for i in range_set:
plt.text(x[i],11, '%d' %y[i])
plt.axis(plot_axis)
plt.show()
Note: There isn't a sanity check for x_max = 0 or >10 implemented, but from your plt.axis([0,5,0,10]) it seems you had this in manually in check anyway.

Python - figure settings in plotting loop

Hi
I have a little problem, I made a loop which creates 3 plots in every iteration and set text
on x label to be rotated but it works only for last fig in a row. I am not sure how to affect first and second figure.
def multi_scatter(x_list, y):
sns.set(style='whitegrid', rc={"grid.linewidth": 0.2})
sns.set_context("paper", font_scale=2)
for x in range(0, len(x_list)):
if x == 0 or x % 3:
chart = sns.pairplot(data=ds_train,
y_vars=[y],
x_vars=[x_list[x], x_list[x+1], x_list[x+2]],
height = 10)
plt.xticks(rotation = 45)
plt.show()
else:
continue
Thank You in advance
This is becuase you defined chart but never extract the axes from chart. You need to specify what the axes are in order to set xticklabels. Try to add these lines in your code (see the inner for loop):
def multi_scatter(x_list, y):
sns.set(style='whitegrid', rc={"grid.linewidth": 0.2})
sns.set_context("paper", font_scale=2)
for x in range(0, len(x_list)):
if x == 0 or x % 3:
chart = sns.pairplot(data=ds_train,
y_vars=[y],
x_vars=[x_list[x], x_list[x+1], x_list[x+2]],
height = 10)
for ax in chart.axes.flat:
ax.tick_params(axis='x', labelrotation=45 )
else:
continue
I did not test it without access to your data, so please let me know if it works!

add label to legend for matplotlib annotation

I am "annotating" many arrows of a certain color to add data to the graph (where events occurred). (example code). Is there a way to add it to the legend? One answer might be to add them manually as I show in the code below, but I guess it is always the last resort. What is the "right" way to do it? (bonus for also having a small arrow mark in the legend)
Here is an example, but really, the example is for ease of use, the question is just how to add label for line.axes.annotate
Here is a code which is almost identical to the one in the link:
A function to add arrows to
def add_arrow(line, position=None, direction='right', size=15, color=None, length=None):
"""
add an arrow to a line.
line: Line2D object
position: x-position of the arrow. If None, mean of xdata is taken
direction: 'left' or 'right'
size: size of the arrow in fontsize points
color: if None, line color is taken.
length: the number of points in the graph the arrow will consider, leave None for automatic choice
"""
if color is None:
color = line.get_color()
xdata = line.get_xdata()
ydata = line.get_ydata()
if not length:
length = max(1, len(xdata) // 1500)
if position is None:
position = xdata.mean()
# find closest index
start_ind = np.argmin(np.absolute(xdata - position))
if direction == 'right':
end_ind = start_ind + length
else:
end_ind = start_ind - length
if end_ind == len(xdata):
print("skipped arrow, arrow should appear after the line")
else:
line.axes.annotate('',
xytext=(xdata[start_ind], ydata[start_ind]),
xy=(xdata[end_ind], ydata[end_ind]),
arrowprops=dict(
arrowstyle="Fancy,head_width=" + str(size / 150), color=color),
size=size
)
A function that uses it
def add_arrows(line, xs, direction='right', size=15, color=None, name=None):
if name:
if color is None:
color = line.get_color()
patch = mpatches.Patch(color=color, label=name, marker="->")
plt.legend(handles=[patch])
for x in xs:
add_arrow(line, x, color=color)
An example to what line is
x,y = [i for i in range(10000)], [i for i in range(10000)]
line = plt.plot(x, y, label="class days")[0]
add_arrows(line, (x,y))
plt.show()

Updating pyplot maker's position on tkinter button click

I am struggling to figure out a way to plot a graph in which values of x axis and y axis comes from two tkinter spinbox where the range of spinbox for the x axis is 125 to 8000 and y axis is -10 to 125, it plots from one point in the graph to the other when a tkinter button is pressed based on the values provided by the spinboxes.
The sample code is:
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import numpy as np
import collections
class PlotClass():
def __init__(self):
fig = Figure(figsize=(5,5),dpi=70,facecolor='cyan')
ax = fig.subplots()
ax.set_xlim(100,9000)
ax.set_ylim(130,-10)
x = [125,250,500,1000,2000,4000,8000]
ticks = [125,250,500,"1K","2K","4K","8K"]
xm = [750,1500,3000,6000]
ax.set_xscale('log', basex=2)
ax.set_xticks(x)
ax.set_xticks(xm, minor=True)
ax.set_xticklabels(ticks)
ax.set_xticklabels([""]*len(xm), minor=True)
ax.yaxis.set_ticks([120,110,100,90,80,70,60,50,40,30,20,10,0,-10])
self.line2,= ax.plot([],[],'-o',markersize=15.0,mew=2)
ax.grid(color="grey")
ax.grid(axis="x", which='minor',color="grey", linestyle="--")
self.canvas = canvas = FigureCanvasTkAgg(fig, master=master)
canvas.show()
canvas.get_tk_widget().grid(column=0,row=2,columnspan=3,rowspan=15)
self.spin = Spinbox(master, from_=125,to=8000,command=self.action)
self.spin.grid(column=5,row=2)
self.spin2 = Spinbox(master, from_=-10,to=125,command=self.action)
self.spin2.grid(column=5,row=3)
self.button = Button(master, text="plot here",command=self.plot)
self.button.grid(column=5,row=4)
def linecreate(self, x=1000,y=20):
X,Y = self.line2.get_data()
if x in X:
ch = list(X)
counti = ch.count(x)
Y[counti] = y
print("Working")
print(Y)
self.canvas.draw_idle()
else:
X = np.append(X,[x])
Y = np.append(Y,[y])
self.line2.set_data(X,Y)
self.canvas.draw_idle()
def plot(self):
self.linecreate(float(self.spin.get()),float(self.spin2.get()))
master = Tk()
plotter = PlotClass()
plotter.ok(125,10)
master.mainloop()
Now the problem is, it needs to check if a particular marker is already in the same x axis, it generally plots a new line to where a new value is added, but I need a way such that when the value of the x axis is 125 and it has already been plotted once to any value like 50 in the y axis, and then it went to plot 500 in x and 90 in y, and finally it again tries to plot 125 in x and 20 in y, the graph creates a marker, but I need to just redraw the old plot with the new Y value without creating a new marker.
In the above code I tried If x in X: to check if the x axis already have been plotted, even though I could check that but I cannot replace the Y axis value with the new value and redraw it.
You forgot to update the plot with self.line2.set_data(X,Y) in case the value already exists.
def linecreate(self, x=1000,y=20):
X,Y = self.line2.get_data()
if x in X:
ch = list(X)
counti = ch.index(x)
Y[counti] = y
else:
X = np.append(X,[x])
Y = np.append(Y,[y])
self.line2.set_data(X,Y)
self.canvas.draw_idle()

how to set "camera position" for 3d plots using python/matplotlib?

I'm learning how to use mplot3d to produce nice plots of 3d data and I'm pretty happy so far. What I am trying to do at the moment is a little animation of a rotating surface. For that purpose, I need to set a camera position for the 3D projection. I guess this must be possible since a surface can be rotated using the mouse when using matplotlib interactively. But how can I do this from a script?
I found a lot of transforms in mpl_toolkits.mplot3d.proj3d but I could not find out how to use these for my purpose and I didn't find any example for what I'm trying to do.
By "camera position," it sounds like you want to adjust the elevation and the azimuth angle that you use to view the 3D plot. You can set this with ax.view_init. I've used the below script to first create the plot, then I determined a good elevation, or elev, from which to view my plot. I then adjusted the azimuth angle, or azim, to vary the full 360deg around my plot, saving the figure at each instance (and noting which azimuth angle as I saved the plot). For a more complicated camera pan, you can adjust both the elevation and angle to achieve the desired effect.
from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(fig)
ax.scatter(xx,yy,zz, marker='o', s=20, c="goldenrod", alpha=0.6)
for ii in xrange(0,360,1):
ax.view_init(elev=10., azim=ii)
savefig("movie%d.png" % ii)
What would be handy would be to apply the Camera position to a new plot.
So I plot, then move the plot around with the mouse changing the distance. Then try to replicate the view including the distance on another plot.
I find that axx.ax.get_axes() gets me an object with the old .azim and .elev.
IN PYTHON...
axx=ax1.get_axes()
azm=axx.azim
ele=axx.elev
dst=axx.dist # ALWAYS GIVES 10
#dst=ax1.axes.dist # ALWAYS GIVES 10
#dst=ax1.dist # ALWAYS GIVES 10
Later 3d graph...
ax2.view_init(elev=ele, azim=azm) #Works!
ax2.dist=dst # works but always 10 from axx
EDIT 1...
OK, Camera position is the wrong way of thinking concerning the .dist value. It rides on top of everything as a kind of hackey scalar multiplier for the whole graph.
This works for the magnification/zoom of the view:
xlm=ax1.get_xlim3d() #These are two tupples
ylm=ax1.get_ylim3d() #we use them in the next
zlm=ax1.get_zlim3d() #graph to reproduce the magnification from mousing
axx=ax1.get_axes()
azm=axx.azim
ele=axx.elev
Later Graph...
ax2.view_init(elev=ele, azim=azm) #Reproduce view
ax2.set_xlim3d(xlm[0],xlm[1]) #Reproduce magnification
ax2.set_ylim3d(ylm[0],ylm[1]) #...
ax2.set_zlim3d(zlm[0],zlm[1]) #...
Minimal example varying azim, dist and elev
To add some simple sample images to what was explained at: https://stackoverflow.com/a/12905458/895245
Here is my test program:
#!/usr/bin/env python3
import sys
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
if len(sys.argv) > 1:
azim = int(sys.argv[1])
else:
azim = None
if len(sys.argv) > 2:
dist = int(sys.argv[2])
else:
dist = None
if len(sys.argv) > 3:
elev = int(sys.argv[3])
else:
elev = None
# Make data.
X = np.arange(-5, 6, 1)
Y = np.arange(-5, 6, 1)
X, Y = np.meshgrid(X, Y)
Z = X**2
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, linewidth=0, antialiased=False)
# Labels.
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
if azim is not None:
ax.azim = azim
if dist is not None:
ax.dist = dist
if elev is not None:
ax.elev = elev
print('ax.azim = {}'.format(ax.azim))
print('ax.dist = {}'.format(ax.dist))
print('ax.elev = {}'.format(ax.elev))
plt.savefig(
'main_{}_{}_{}.png'.format(ax.azim, ax.dist, ax.elev),
format='png',
bbox_inches='tight'
)
Running it without arguments gives the default values:
ax.azim = -60
ax.dist = 10
ax.elev = 30
main_-60_10_30.png
Vary azim
The azimuth is the rotation around the z axis e.g.:
0 means "looking from +x"
90 means "looking from +y"
main_-60_10_30.png
main_0_10_30.png
main_60_10_30.png
Vary dist
dist seems to be the distance from the center visible point in data coordinates.
main_-60_10_30.png
main_-60_5_30.png
main_-60_20_-30.png
Vary elev
From this we understand that elev is the angle between the eye and the xy plane.
main_-60_10_60.png
main_-60_10_30.png
main_-60_10_0.png
main_-60_10_-30.png
Tested on matpotlib==3.2.2.
Try the following code to find the optimal camera position
Move the viewing angle of the plot using the keyboard keys as mentioned in the if clause
Use print to get the camera positions
def move_view(event):
ax.autoscale(enable=False, axis='both')
koef = 8
zkoef = (ax.get_zbound()[0] - ax.get_zbound()[1]) / koef
xkoef = (ax.get_xbound()[0] - ax.get_xbound()[1]) / koef
ykoef = (ax.get_ybound()[0] - ax.get_ybound()[1]) / koef
## Map an motion to keyboard shortcuts
if event.key == "ctrl+down":
ax.set_ybound(ax.get_ybound()[0] + xkoef, ax.get_ybound()[1] + xkoef)
if event.key == "ctrl+up":
ax.set_ybound(ax.get_ybound()[0] - xkoef, ax.get_ybound()[1] - xkoef)
if event.key == "ctrl+right":
ax.set_xbound(ax.get_xbound()[0] + ykoef, ax.get_xbound()[1] + ykoef)
if event.key == "ctrl+left":
ax.set_xbound(ax.get_xbound()[0] - ykoef, ax.get_xbound()[1] - ykoef)
if event.key == "down":
ax.set_zbound(ax.get_zbound()[0] - zkoef, ax.get_zbound()[1] - zkoef)
if event.key == "up":
ax.set_zbound(ax.get_zbound()[0] + zkoef, ax.get_zbound()[1] + zkoef)
# zoom option
if event.key == "alt+up":
ax.set_xbound(ax.get_xbound()[0]*0.90, ax.get_xbound()[1]*0.90)
ax.set_ybound(ax.get_ybound()[0]*0.90, ax.get_ybound()[1]*0.90)
ax.set_zbound(ax.get_zbound()[0]*0.90, ax.get_zbound()[1]*0.90)
if event.key == "alt+down":
ax.set_xbound(ax.get_xbound()[0]*1.10, ax.get_xbound()[1]*1.10)
ax.set_ybound(ax.get_ybound()[0]*1.10, ax.get_ybound()[1]*1.10)
ax.set_zbound(ax.get_zbound()[0]*1.10, ax.get_zbound()[1]*1.10)
# Rotational movement
elev=ax.elev
azim=ax.azim
if event.key == "shift+up":
elev+=10
if event.key == "shift+down":
elev-=10
if event.key == "shift+right":
azim+=10
if event.key == "shift+left":
azim-=10
ax.view_init(elev= elev, azim = azim)
# print which ever variable you want
ax.figure.canvas.draw()
fig.canvas.mpl_connect("key_press_event", move_view)
plt.show()
Q: How can I set view in matplotlib?
For a 3d plot, how do you fixate the view?
A: By setting properties ax.azim and ax.level
ax.elev = 0
ax.azim = 270 # xz view
ax.elev = 0
ax.azim = 0 # yz view
ax.elev = 0
ax.azim = -90 # xy view

Categories

Resources