admin管理员组文章数量:1122846
I can do the following in base R
plotting.
x1 <- c(3, 4)
y1 <- c(5, 8)
x2 <- c(2, 5)
y2 <- c(5, 7)
plot(x1, y1, type = 'l')
lines(x2, y2, col = 'red')
The key point is that, when I call that final lines
command, the plotting function has already fixed the x-axis.
How can I do this in matplotlib
to superimpose a new set of data on top of the current axis graph while keeping the x-axis range the same as before but allowing the y-axis range to accommodate the vertical range of the new points?
The code below fails because it expands the x-axis.
import matplotlib.pyplot as plt
x1 = [3, 4]
y1 = [5, 8]
x2 = [2, 5]
y2 = [4, 9]
fig, ax = plt.subplots()
ax.plot(x1, y1)
ax.plot(x2, y2)
plt.show()
plt.close()
The code below fails by cropping a bit too tight compared to how just a plot of x1
and y1
would be.
fig, ax = plt.subplots()
ax.plot(x1, y1)
ax.plot(x2, y2)
ax.set_xlim([min(x1), max(x1)])
plt.show()
plt.close()
The code below has a little bit of padding to the right and to the left of the maximum and minmum values of x1
but does not superimpose the orange line for x2
and y2
.
fig, ax = plt.subplots()
ax.plot(x1, y1)
# ax.plot(x2, y2)
# ax.set_xlim([min(x1), max(x1)])
plt.show()
plt.close()
So how can I get matplotlib
to superimpose the second set of data on top of the original graph while allowing the vertical axis to expand to include new y-values, yet keep the x-axis at the original range that contains a bit of padding to the right and the left?
I can do the following in base R
plotting.
x1 <- c(3, 4)
y1 <- c(5, 8)
x2 <- c(2, 5)
y2 <- c(5, 7)
plot(x1, y1, type = 'l')
lines(x2, y2, col = 'red')
The key point is that, when I call that final lines
command, the plotting function has already fixed the x-axis.
How can I do this in matplotlib
to superimpose a new set of data on top of the current axis graph while keeping the x-axis range the same as before but allowing the y-axis range to accommodate the vertical range of the new points?
The code below fails because it expands the x-axis.
import matplotlib.pyplot as plt
x1 = [3, 4]
y1 = [5, 8]
x2 = [2, 5]
y2 = [4, 9]
fig, ax = plt.subplots()
ax.plot(x1, y1)
ax.plot(x2, y2)
plt.show()
plt.close()
The code below fails by cropping a bit too tight compared to how just a plot of x1
and y1
would be.
fig, ax = plt.subplots()
ax.plot(x1, y1)
ax.plot(x2, y2)
ax.set_xlim([min(x1), max(x1)])
plt.show()
plt.close()
The code below has a little bit of padding to the right and to the left of the maximum and minmum values of x1
but does not superimpose the orange line for x2
and y2
.
fig, ax = plt.subplots()
ax.plot(x1, y1)
# ax.plot(x2, y2)
# ax.set_xlim([min(x1), max(x1)])
plt.show()
plt.close()
So how can I get matplotlib
to superimpose the second set of data on top of the original graph while allowing the vertical axis to expand to include new y-values, yet keep the x-axis at the original range that contains a bit of padding to the right and the left?
2 Answers
Reset to default 2Your second approach is quite close to what I think you want. You just need to store the original x-limits and then use those to reset the plot after it's resized:
import matplotlib.pyplot as plt
x1 = [3, 4]
y1 = [5, 8]
x2 = [2, 5]
y2 = [4, 9]
fig, ax = plt.subplots()
ax.plot(x1, y1)
xlim = ax.get_xlim()
ax.plot(x2, y2)
ax.set_xlim(xlim)
plt.show()
plt.close()
This solution locks the x-axis range based on the initial plot of x1, y1 and superimposes the second plot of x2, y2, ensuring the x-axis remains unchanged while allowing the y-axis to adjust dynamically.:
# Data for first and second lines
x1, y1 = [3, 4], [5, 8]
x2, y2 = [2, 5], [4, 9]
# Create the plot
fig, ax = plt.subplots()
# Plot the first set of data
ax.plot(x1, y1, color='grey', label='Line 1')
Store the original x-axis limits
xlim = ax.get_xlim()
# Plot the second set of data
ax.plot(x2, y2, color='red', label='Line 2')
Reset the x-axis limits to the original values
ax.set_xlim(xlim)
Plot
ax.legend()
plt.show()
plt.close()
本文标签: pythonLocking matplotlib xaxis range and then plotting on top of itStack Overflow
版权声明:本文标题:python - Locking `matplotlib` x-axis range and then plotting on top of it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303976a1932068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
r
-question. Although you comparer
andpython
, the answer should come frompython
. – Friede Commented Nov 22, 2024 at 11:50