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

如何调整Matplotlib X轴刻度以正确显示橙色、蓝色曲线及全部图像

问题描述

我无法正确绘制橙色、蓝色两条曲线,已查阅相关文档,多次尝试调整参数仍未得到预期效果。请问如何才能让这两条曲线和其他曲线一同在同一张图中正常显示?
示例效果图
我当前运行得到的效果如下:
当前运行效果图
我的代码如下:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0.264, 0.331, 0.397, 0.417, 0.438, 0.45, 0.459, 0.466])
y = np.array([0.01, 0.1, 1, 2, 4, 6, 8, 10])
# creating scatter plot with both negative
# and positive axes
plt.plot(x, y, marker='o')
x = np.array([0.375, 0.435, 0.494, 0.512, 0.53, 0.541, 0.548, 0.564])
y = np.array([0.01, 0.1, 1, 2, 4, 6, 8, 10])
plt.plot(x, y, marker='o')
x2points=np.array([-3.672, -3.733, -3.793, -3.811, -3.828, -3.839, -3.846, -3.852])
y2points = np.array([-0.01, -0.1, -1, -2, -4, -6, -8, -10])
plt.plot(x2points, y2points, marker = 'o')
x2points = np.array([-14.86, -14.931, -14.998, -15.018, -15.039, -15.051, -15.06, -15.067])
y2points = np.array([-0.01, -0.1, -1, -2, -4, -6, -8, -10])
plt.plot(x2points, y2points, marker='o')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')

# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
plt.xticks([-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1])
plt.yticks([-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# visualizing the plot using plt.show() function
plt.show()

我想要实现的最终效果如下:
目标效果图

解决方案

你的四条曲线实际都已经正常渲染,问题出在你设置的x轴正半轴范围仅到1,而两条正半轴曲线的x值都在0.2~0.6区间内,导致视觉上被压缩在靠近y轴的极小区域,和预期效果不符。
你只需要把正半轴两条曲线的x值调整为和负半轴对称的正值,同时修改x轴刻度范围即可,修改后的代码如下:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([3.672, 3.733, 3.793, 3.811, 3.828, 3.839, 3.846, 3.852])
y = np.array([0.01, 0.1, 1, 2, 4, 6, 8, 10])
plt.plot(x, y, marker='o')
x = np.array([14.86, 14.931, 14.998, 15.018, 15.039, 15.051, 15.06, 15.067])
y = np.array([0.01, 0.1, 1, 2, 4, 6, 8, 10])
plt.plot(x, y, marker='o')
x2points=np.array([-3.672, -3.733, -3.793, -3.811, -3.828, -3.839, -3.846, -3.852])
y2points = np.array([-0.01, -0.1, -1, -2, -4, -6, -8, -10])
plt.plot(x2points, y2points, marker = 'o')
x2points = np.array([-14.86, -14.931, -14.998, -15.018, -15.039, -15.051, -15.06, -15.067])
y2points = np.array([-0.01, -0.1, -1, -2, -4, -6, -8, -10])
plt.plot(x2points, y2points, marker='o')

plt.axvline(0, c='black', ls='--')
plt.axhline(0, c='black', ls='--')
plt.xticks([-15,-10,-5,0,5,10,15])
plt.yticks([-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

plt.show()

运行上述代码即可得到四象限各分布一条曲线的目标效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 03:54:07