You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matplotlib的3D绘图中反转Y轴使其与X轴共享原点

How to Invert Y-Axis in Matplotlib 3D Plot to Share Origin with X-Axis

Hey there, let's get this sorted out. You've already nailed inverting the X-axis in your 3D Matplotlib plot, and now you want the Y-axis to mirror that so both share the same origin—totally doable.

First, let's start with a refined version of your code that should get you the desired result:

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d

# Set up the figure and 3D axes
fig = plt.figure(figsize=(8, 6))
ax = plt.gca(projection='3d')

# Add plot labels and title
ax.set_title('3d_image_show', fontsize=14)
ax.set_xlabel('X', fontsize=12)
ax.set_ylabel('Y', fontsize=12)
ax.set_zlabel('Z', fontsize=12)

# Adjust the view angle to align axes visually
ax.view_init(elev=20, azim=45)

# Invert both axes to share the origin
ax.invert_xaxis()
ax.invert_yaxis()

# Optional: Lock axis ranges if your data has different scales
# Uncomment below to force X and Y to start at the same point
# ax.set_xlim(0, max_your_x_value)
# ax.set_ylim(0, max_your_y_value)

plt.tight_layout()
plt.show()

Key Notes to Make This Work:

  • invert_yaxis() is the right call: You already included this, but sometimes the 3D perspective can make it look like the axes aren't aligned. Tweaking the azim parameter in view_init() (try values like 45, 135, or -45) can help the origin alignment pop visually.
  • Match axis ranges: If your X and Y data have different min/max values, explicitly setting set_xlim() and set_ylim() to the same range ensures both axes start at the exact same origin point, no visual misalignment.

Here's what your expected output should look like:
Expected 3D Plot with Inverted X & Y Axes Sharing Origin

内容的提问来源于stack exchange,提问作者proceeding

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:32:30