How to make an Axes transparent to events? - python-3.x

I am trying to find a way to make an Axes object passthrough for events.
For context, I have a figure with 6 small subplots. Each of them responds to mouse motion events by displaying a cursor dot and text info where the user aims. I also made it so that clicking a subplot will make it as large as the figure for better visibility. When moving the mouse over invisible axes, event.inaxes will still point to that ax despite being set to invisible and that is what I would like to avoid.
Below is the MRE:
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_gid("ax1")
ax2.set_gid("ax2")
fig.show()
def on_movement(event):
"""Write on the figure in which `Axes`object the event happened."""
width, height = fig.bbox.bounds[2:]
x = event.x/width
y = event.y/height
text = ax.get_gid() if (ax := event.inaxes) is not None else "None"
fig.texts[:] = []
fig.text(x, y, s=text, transform=fig.transFigure, c="white", bbox=dict(fc="#0055AA", ec="black"))
fig.canvas.draw()
fig.canvas.mpl_connect("motion_notify_event", on_movement)
As expected, as you hover the mouse over ax1, the empty gap and ax2, you will see one of those three texts appear:
ax1.set_position((1/3, 1/3, 2/3, 2/3))
Same thing as I arbitrarily resize and move ax1 so that it is partly overlaps with ax2.
ax2.set_visible(False)
Now this is my problem. Invisible axes still trigger events. Is there a way to make some axes "transparent" to events? Obviously the usual technique of sorting all the cases in the callback does not work here.
Currently envisaged solutions:
ideally, finding a setting akin to zorder so that the "highest" axes gets the event.
ugly workaround: set the position of the invisible axes to ((0, 0, 1e-10, 1e-10)).
less ugly: working with figure coordinates to convert event.x, event.y into event.xdata, event.ydata for the only ax that I know is visible. Basically xdata1, ydata1 = ax1.transAxes.inverted().transform((event.x, event.y)) if event.inaxes is not None + see if there are edge cases.
The latter is already implemented and does work, so save your time if you want to write a reply using that approach. I'm mostly interested in an amazing one-liner that I would have missed, something like ax2.set_silenced(True).
Python 3.8.5
Matplotlib 3.1.3

Well, setting the appropriate zorder does work actually.
ax1.set_zorder(2)
ax2.set_zorder(1)
...
def on_movement(event):
...
fig.text(x, y, ..., zorder=1000)
...

Related

How to plot hyperparameter tuning results?

I have the result of a grid search as follows.
"trial","learning_rate","batch_size","accuracy","f1","loss"
1,0.000007,70,0.789,0.862,0.467
2,0.000008,100,0.710,0.822,0.563
3,0.000008,90,0.823,0.874,0.524
4,0.000007,90,0.833,0.878,0.492
5,0.000009,110,0.715,0.825,0.509
6,0.000006,90,0.883,0.885,0.932
7,0.000009,80,0.850,0.895,0.408
8,0.000006,110,0.683,0.812,0.593
9,0.000005,90,0.769,0.848,0.468
10,0.000005,80,0.816,0.868,0.462
11,0.000003,100,0.852,0.901,0.448
12,0.000004,100,0.705,0.818,0.512
13,0.000003,110,0.708,0.818,0.567
14,0.000002,90,0.683,0.812,0.552
15,0.000008,100,0.791,0.857,0.438
16,0.000006,110,0.683,0.812,0.604
17,0.000007,70,0.693,0.816,0.592
18,0.000005,110,0.830,0.883,0.892
19,0.000004,90,0.693,0.816,0.591
20,0.000008,70,0.696,0.818,0.570
I want to create a plot more or less similar to this using matplotlib. I know this is plotted using weights and biases but I cannot use that.
Though I don't care for the inference part. I just want the plot. I've been trying to do this using twinx but have not been successful. This is what I have so far.
from csv import DictReader
import matplotlib.pyplot as plt
trials = list(DictReader(open("hparams_trials.csv")))
trials = {f"trial_{trial['trial']}": [int(trial["batch_size"]),
float(trial["f1"]),
float(trial["loss"]),
float(trial["accuracy"]),
float(trial["learning_rate"])] for trial in trials}
items = ["batch_size", "f1", "loss", "accuracy", "learning_rate"]
host_y_values_index = 0
parts_y_values_indexes = [1, 2, 3, 4]
fig, host = plt.subplots(figsize=(8, 5)) # (width, height) in inches
fig.dpi = 300. # Figure resolution
# Removing extra spines
host.spines.top.set_visible(False)
host.spines.bottom.set_visible(False)
host.spines.right.set_visible(False)
# Creating subplots which share the same x axis.
parts = {index: host.twinx() for index in parts_y_values_indexes}
# Setting the limits of the host plot
host.set_xlim(0, len(trials["trial_1"]))
host.set_ylim(min([i[host_y_values_index] for i in trials.values()]),
max([i[host_y_values_index] for i in trials.values()]))
# Removing the extra spines from the other plots and setting y limits
for part in parts_y_values_indexes:
parts[part].spines.top.set_visible(False)
parts[part].spines.bottom.set_visible(False)
parts[part].set_ylim(min([trial[part] for trial in trials.values()]),
max([trial[part] for trial in trials.values()]))
# Colors of the trials
colors = ["gold", "lightcoral", "maroon", "springgreen", "cyan", "steelblue", "darkmagenta", "fuchsia", "crimson",
"lime", "mediumblue", "cadetblue", "dodgerblue", "olivedrab", "sandybrown", "bisque", "orangered", "black",
"rosybrown", "chocolate"]
# The plots
plots = []
# Plotting the trials. This is where I'm having problems with.
for index, trial in enumerate(trials):
plots.append(host.plot(items, trials[trial], color=colors[index], label=trial)[0])
# Creating the legend
host.legend(handles=plots, fancybox=True, loc='right', facecolor="snow", bbox_to_anchor=(1.02, 0.495), framealpha=1)
# Defining the positions of the spines.
spines_positions = [-104.85 * i for i in parts_y_values_indexes]
# Repositioning the spines
for part in parts_y_values_indexes:
parts[part].spines['right'].set_position(('outward', spines_positions[-part]))
# Adjust spacings around fig
fig.tight_layout()
host.grid(True)
# This is better than the one above but it appears on top of the legend.
# plt.grid(True)
plt.draw()
plt.show()
I'm having several problems with that code. First, I cannot place each value of a single trial based on a different spine and then connect them to one another. What I mean is that each trial has a batch size, an f1, a loss, accuracy and a learning rate. Each of those need to be plotted based on their own spine while connected to each other in that order. However, I cannot plot them based their dedicated spines and then connect them to one another to have a line plot per trial. Accordingly, for now I have placed everything in the host plot but I know that is wrong and have no idea what the correct approach is. Second problem, the ticks of the learning rate change. It gets shown as a range of 2 to 9 and then a 1e-6 appears at the top. I want to keep the original value. Third problem is probably part of the second one. The 1e-6 appears at the top right above the legend rather than above the spine for some reason. I'm struggling with resolving all three of these problems and would appreciate any help anyone can provide. If what I am doing is totally wrong, please help me in finding the correct solution. I'm somewhat going in circles here and haven't been able to find any working solutions so far.

Is it possible to extract the default tick locations from the primary axis and pass it to a secondary access with matplotlib?

When making a plot with with
fig, ax = plt.subplots()
x=[1,2,3,4,5,6,7,8,9,10]
y=[1,2,3,4,5,6,7,8,9,10]
ax.plot(x,y)
plt.show()
matplotlib will determine the tick spacing/location and value of the tick. Is there are way to extract this automatic spacing/location AND the value? I want to do this so i can pass it to
set_xticks()
for my secondary axis (using twiny()) then use set_ticklabels() with a custom label. I realise I could use secondary axes giving both a forward and inverse function however providing an inverse function is not feasible for the goal of my code.
So in the image below, the ticks are only showing at 2,4,6,8,10 rather than all the values of x and I want to somehow extract these values and position so I can pass to set_xticks() and then change the tick labels (on a second x axis created with twiny).
UPDATE
When using the fix suggested it works well for the x axis. However, it does not work well for the y-axis. For the y-axis it seems to take the dataset values for the y ticks only. My code is:
ax4 = ax.twinx()
ax4.yaxis.set_ticks_position('left')
ax4.yaxis.set_label_position('left')
ax4.spines["left"].set_position(("axes", -0.10))
ax4.set_ylabel(self.y_2ndary_label, fontweight = 'bold')
Y = ax.get_yticks()
ax4.yaxis.set_ticks(Y)
ax4.yaxis.set_ticklabels( Y*Y )
ax4.set_ylim(ax.get_ylim())
fig.set_size_inches(8, 8)
plt.show()
but this gives me the following plot. The plot after is the original Y axis. This is not the case when I do this on the x-axis. Any ideas?
# From "get_xticks" Doc: The locations are not clipped to the current axis limits
# and hence may contain locations that are not visible in the output.
current_x_ticks = ax.get_xticks()
current_x_limits = ax.get_xlim()
ax.set_yticks(current_x_ticks) # Use this before "set_ylim"
ax.set_ylim(current_x_limits)
plt.show()

specify the lat/lon label location in cartopy (remove at some sides)

The new capability in Cartopy 0.18.0 to add lat/lon labels for any map projection is excellent. It's a great addition to this package. For some maps, especially in polar regions, the lat/lon labels can be very crowded. Here is an example.
from matplotlib import pyplot as plt
import numpy as np
import cartopy.crs as ccrs
pcproj = ccrs.PlateCarree()
lon0 = -150
mapproj = ccrs.LambertAzimuthalEqualArea(
central_longitude=lon0,central_latitude=75,
)
XLIM = 600e3; YLIM=700e3
dm =5; dp=2
fig = plt.figure(0,(7,7))
ax = fig.add_axes([0.1,0.1,0.85,0.9],projection=mapproj)
ax.set_extent([-XLIM,XLIM,-YLIM,YLIM],crs=mapproj)
ax.coastlines(resolution='50m',color='.5',linewidth=1.5)
lon_grid = np.arange(-180,181,dm)
lat_grid = np.arange(-80,86,dp)
gl = ax.gridlines(draw_labels=True,
xlocs=lon_grid,ylocs=lat_grid,
x_inline=False,y_inline=False,
color='k',linestyle='dotted')
gl.rotate_labels = False
Here is the output plot: I can't embed image yet, so here is the link
What I am looking for is to have lat labels on the left and right sides and lon labels at the bottom, with no labels at the top. This can be easily done in Basemap using a list of flags. I am wondering if this is possible with cartopy now.
Several failed attempts:
I came across a Github open issue for cartopy on a similar topic, but the suggested method does not work for this case. Adding gl.ylocator = mticker.FixedLocator(yticks) does nothing and adding gl.xlocator = mticker.FixedLocator(xticks) gets rid of most of lon labels except the 180 line on left and right sides but all the other lon labels are missing. The 80N lat label is still on the top, see here. After a more careful read of that thread, it seems it is still an ongoing effort for future cartopy releases.
Using gl.top_labels=False does not work either.
Setting y_inline to True makes the lat labels completely gone. I guess this might be because of axes extent I used. The lat labels might be on some longitude lines outside of the box. This is a separate issue, about how to specify the longitude lines/locations of the inline labels.
Right now, I have chosen to turn off the labels. Any suggestions and temporary solutions will be appreciated. At this point, the maps such as the examples above are useful for quicklooks but not ready for any formal use.
UPDATE:
Based on #swatchai 's suggestion, there is a temporary workaround below:
# --- add _labels attribute to gl
plt.draw()
# --- tol is adjusted based on the positions of the labels relative to the borders.
tol = 20
for ea in gl._labels:
pos = ea[2].get_position()
t_label = ea[2].get_text()
# --- remove lon labels on the sides
if abs(abs(pos[0])-XLIM)<tol:
if 'W' in t_label or 'E' in t_label or '180°' in t_label:
print(t_label)
ea[2].set_text('')
# --- remove labels on top
if abs(pos[1]-YLIM)<tol:
ea[2].set_text('')
This is almost what I wanted except that the 74N labels are missing because it is close to the 170W labels on the sides and cartopy chose 170W label instead of 74N. So I need a little more simple tweaks to put it back there.
This could be a workaround for your project until a better solution comes up.
# more code above this line
# this suppresses drawing labels on top edges
# only longitude_labels disappear, some latitude_labels persist
gl.top_labels=False
# workaround here to manipulate the remaining labels
plt.draw() #enable the use of ._lables()
for ea in gl._labels:
#here, ea[2] is a Text object
#print(ea)
if '80°N'==ea[2].get_text():
# set it a blank string
ea[2].set_text("")
ax.set_title("No Labels on Top Edge");
plt.show()
The output plot:

Bar format (kind) is not displaying the right plot (matplotlib)

I need to do a plot using three variables. One of them should be on the secondary Y axis in bar format (kind), the remaining variables (two) should be on the left axis using a simple line. However, I got the following chart:
When I use the three variables in line format I get the right plot (which is not very useful for a visual analysis):
I did a quick test using a small sample from my data (code below). I get the right pic when I use bar format for the third one.
I wonder, what is going on? Is there a problem with the data size (which I dont think so bcs I get less than 100 rows)?
df2 = pd.DataFrame({'ind':[120.29, 125.45, 127.37, 130.39, 128.30],
'var1':[129.907990, 129.571185, 129.234380, 128.897574, 128.560769],
'var2':[-0.074037, -0.031806, -0.014426, 0.011578, -0.002028]})
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
df2['ind'].plot(ax=ax1)
df2['var1'].plot(ax=ax1)
df2['var2'].plot(kind='bar', ax=ax2, color='r')
plt.show()
PD: In addition, I noted that in the third pic the line is behind the bar. How can I change that?
I found the solution for this (this link helped me a lot ). Basically, it is based on the index you set up previously.
This is the new code:
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(df2.index, df2['ind'])
ax1.plot(df2.index, df2['var1'])
ax2.bar(df2.index, df2['var2'], color='r')
plt.show()
Hope this helps.

Rotate figure - Flopy

I am using functions, e.x: ml.dis.top.plot(). I would like to rotate these figures and delete titles. How can I do that?
plt.title('') seems to work for titles but I cannot rotate these figures. Here is the part of the script:
fig = plt.figure(figsize=(75, 75))
plt.subplot(1,1,1,aspect='equal')
mf.dis.top.plot(contour=True, colorbar=True)
plt.title('')
plt.savefig('top_plot.png')
So there are different ways that you can make model plots in flopy. You are using the quick and easy way to plot one of our arrays. What you probably want to do is use the ModelMap capability, which is described in https://github.com/modflowpy/flopy/blob/develop/examples/Notebooks/flopy3_MapExample.ipynb. This will give you full control over your figure, including rotation and offset and will allow you to customize the title and anything else you'll need to do. The code might look something like the following:
fig = plt.figure(figsize=(75, 75))
ax = plt.subplot(1, 1, 1, aspect='equal')
modelmap = flopy.plot.ModelMap(model=mf, rotation=14)
modelmap.contour_array(mf.dis.top.array)
plt.savefig('top_plot.png')

Resources