Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: improper y-limit in Stream/Trace plot when 0-value trace exists#3665

Open
Dengda98 wants to merge 3 commits intoobspy:masterfrom
Dengda98:plot
Open

FIX: improper y-limit in Stream/Trace plot when 0-value trace exists#3665
Dengda98 wants to merge 3 commits intoobspy:masterfrom
Dengda98:plot

Conversation

@Dengda98
Copy link

What does this PR do?

Hi ObsPy !

To explain this bug, see the tiny script below,

from obspy import *
st = read()
tr2 = st[0].copy()
tr2.data[:] = 0.0    # all zero
tr2.stats.channel += '2'  # just for distinguish
for tr in st:
    tr.data[:] *= 1e-8
st += Stream(tr2)
st.plot()
image

In this figure, the y-limit of all axes are set to [-0.05, 0.05], which is probably the default margin used by matplotlib when plotting all-zero data. If the range of other traces is smaller than [-0.05, 0.05], an improper y-limit will be set in the function WaveformPlotting.__plot_set_y_ticks().

To solve this, I just add if-statements to set an extremely small y-limit for this specific issure. This issue might be extremely unlikely to occur in real data, but I encountered it during my synthetic seismogram research, so I submitted this PR.

PR Checklist

  • Correct base branch selected? master for new features, maintenance_... for bug fixes
  • This PR is not directly related to an existing issue (which has no PR yet).
  • Add the yellow ready for review label when you are ready for the PR to be reviewed.

@ThomasLecocq
Copy link
Contributor

What happens when you plot equal_scale=False to the plot command?

@Dengda98
Copy link
Author

@ThomasLecocq

The plot with equal_scale=False looks fine, probably the default ylim issure only affect this part.

if self.equal_scale:
ylims = np.vstack([ax.get_ylim() for ax in self.axis])
yranges = np.diff(ylims).flatten()
yrange_max = yranges.max()
yrange_paddings = -yranges + yrange_max
ylims[:, 0] -= yrange_paddings[:] / 2
ylims[:, 1] += yrange_paddings[:] / 2
for ax, ylims_ in zip(self.axis, ylims):
ax.set_ylim(*ylims_)

@ThomasLecocq ThomasLecocq added the .imaging Issues with our plotting functionalities label Dec 24, 2025
@ThomasLecocq
Copy link
Contributor

@Dengda98 didn't forget you :) Just trying to get my ideas together on how to approach this in the most generalized way (your idea is very valid, trying to think if it could happen e.g. if not "all zeros", but "all the same value", for example, etc?)

@Dengda98
Copy link
Author

Dengda98 commented Jan 6, 2026

@ThomasLecocq Thanks!
I test "all the same value", the issue also exists. An improper y-range will cover the normal y-range.
I commit a new solution, but haven't found a generalized or elegant way.


Bug reproduce (same value)

from obspy import *
st = read()
tr2 = st[0].copy()
tr2.data[:] = 1e8    # all the same value, and large enough to cover the normal data
tr2.stats.channel += '2'  # just for distinguish
# for tr in st:
#     tr.data[:] *= 1e-8
st += Stream(tr2)
st.plot()
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.imaging Issues with our plotting functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments