Drawing rectangle on top of data using patches

2024/11/16 14:43:12

I am trying to draw a rectangle on top of a data plot in matplotlib. To do this, I have this code

import matplotlib.patches as patches
import matplotlib.pyplot as pl...fig = pl.figure()
ax=fig.add_axes([0,0,1,1])
ax.add_patch(
patches.Rectangle((776820, 5000),   # (x,y)3000,          # width3500,          # heightfill=False)
)
ax.plot(signal)
ax.plot(fit)
...

When I do this, the rectangle is behind the data, however. It doesn't appear to matter if I add the rectnagle before or after plotting the actual data. How can I ensure that the rectangle is the top-most element in the figure?

Answer

The matplotlib.patches.Rectangle allows the keyword argument zorder that is by default 1.0.

Choosing a zorder above one should bring your rectangle to the foreground of the image.

ax.add_patch(
patches.Rectangle((776820, 5000),   # (x,y)3000,          # width3500,          # heightfill=False,zorder=2)
)
https://en.xdnf.cn/q/71660.html

Related Q&A

Setting row edge color of matplotlib table

Ive a pandas DataFrame plotted as a table using matplotlib (from this answer).Now I want to set the bottom edge color of a given row and Ive this code:import pandas as pd import numpy as np import matp…

TypeError: string indices must be integers (Python) [duplicate]

This question already has answers here:Why am I seeing "TypeError: string indices must be integers"?(10 answers)Closed 5 years ago.I am trying to retrieve the id value : ad284hdnn.I am getti…

how to split numpy array and perform certain actions on split arrays [Python]

Only part of this question has been asked before ([1][2]) , which explained how to split numpy arrays. I am quite new in Python. I have an array containing 262144 items and want to split it in small…

NLTK was unable to find the java file! for Stanford POS Tagger

I have been stuck trying to get the Stanford POS Tagger to work for a while. From an old SO post I found the following (slightly modified) code:stanford_dir = C:/Users/.../stanford-postagger-2017-06-09…

Append a list in Google Sheet from Python

I have a list in Python which I simply want to write (append) in the first column row-by-row in a Google Sheet. Im done with all the initial authentication part, and heres the code:credentials = Google…

Compute linear regression standardized coefficient (beta) with Python

I would like to compute the beta or standardized coefficient of a linear regression model using standard tools in Python (numpy, pandas, scipy.stats, etc.).A friend of mine told me that this is done in…

Individually labeled bars for bar graph in Plotly

I was trying to create annotations for grouped bar charts - where each bar has a specific data label that shows the value of that bar and is located above the centre of the bar.I tried a simple modific…

Is there a way to subclass a generator in Python 3?

Aside from the obvious, I thought Id try this, just in case:def somegen(input=None):...yield...gentype = type(somegen()) class subgen(gentype):def best_function_ever():...Alas, Pythons response was qui…

represent binary search trees in python

how do i represent binary search trees in python?

Python os.path.commonprefix - is there a path oriented function?

So I have this python code:print os.path.commonprefix([rC:\root\dir,rC:\root\dir1])Real ResultC:\root\dirDesired resultC:\rootQuestion 1Based on os.path.commonprefix documentation: Return the longest p…