首页 > 解决方案 > Matplotlib 绘图未显示所有 xticks 和 yticks

问题描述

我在 matplotlib 中创建子图,但并未显示所有 xticks 和 yticks。我已经尝试了一切,从设置 xlim 和 ylim,链接图形大小等。问题是这是对 hackerrnak 的动手操作,他们正在根据他们的预期输出评估我的输出。xaxis 上的 0.0 和 yaxis 上的 1.0 根本不匹配。我在这里做错了什么。

这是代码,

import matplotlib.pyplot as plt
import numpy as np

def test_generate_figure2():
    np.random.seed(1000)
    x = np.random.rand(10)
    y = np.random.rand(10)
    z = np.sqrt(x**2 + y**2)
    fig = plt.figure(figsize=(8,6))
    axes1 = plt.subplot(2, 2, 1, title="Scatter plot with Upper Triangle Markers")
    axes1.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes1.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes1.set_ylim(-0.2,1.0) #Doing this still doesnot get the expected output
    axes1.set_xlim(0.0,1.2)
    print(axes1.get_yticks())
    axes1.scatter(x, y, marker="^", s=80, c=z)
    
    axes2 = plt.subplot(2, 2, 2, title="Scatter plot with Plus Markers")
    axes2.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes2.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes2.scatter(x, y, marker="+", s=80, c=z)
    
    axes3 = plt.subplot(2, 2, 3, title="Scatter plot with Circle Markers")
    axes3.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes3.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes3.scatter(x, y, marker="o", s=80, c=z)
    
    axes4 = plt.subplot(2, 2, 4, title="Scatter plot with Diamond Markers")
    axes4.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes4.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes4.scatter(x, y, marker="d", s=80,c=z)
    
    plt.tight_layout()
    plt.show()
    
test_generate_figure2()

我的输出, 我的输出 预期输出, 预期输出

标签: python-3.xmatplotlib

解决方案


您的set_xlim&set_ylim方法有效。您只需要为每个子图设置它:

https://akuiper.com/console/5vaLIq0ZC_KO

import matplotlib.pyplot as plt
import numpy as np

def test_generate_figure2():
    np.random.seed(1000)
    x = np.random.rand(10)
    y = np.random.rand(10)
    z = np.sqrt(x**2 + y**2)
    fig = plt.figure(figsize=(8,6))
    axes1 = plt.subplot(2, 2, 1, title="Scatter plot with Upper Triangle Markers")
    axes1.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes1.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes1.set_ylim(-0.2,1.0) #Doing this still doesnot get the expected output
    axes1.set_xlim(0.0,1.2)
    print(axes1.get_yticks())
    axes1.scatter(x, y, marker="^", s=80, c=z)

    axes2 = plt.subplot(2, 2, 2, title="Scatter plot with Plus Markers")
    axes2.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes2.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes2.set_ylim(-0.2,1.0) #Doing this still doesnot get the expected output
    axes2.set_xlim(0.0,1.2)

    axes2.scatter(x, y, marker="+", s=80, c=z)

    axes3 = plt.subplot(2, 2, 3, title="Scatter plot with Circle Markers")
    axes3.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes3.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes3.set_ylim(-0.2,1.0) #Doing this still doesnot get the expected output
    axes3.set_xlim(0.0,1.2)

    axes3.scatter(x, y, marker="o", s=80, c=z)

    axes4 = plt.subplot(2, 2, 4, title="Scatter plot with Diamond Markers")
    axes4.set_xticks([0.0, 0.4, 0.8, 1.2])
    axes4.set_yticks([-0.2, 0.2, 0.6, 1.0])
    axes4.set_ylim(-0.2,1.0) #Doing this still doesnot get the expected output
    axes4.set_xlim(0.0,1.2)

    axes4.scatter(x, y, marker="d", s=80,c=z)

    plt.tight_layout()
    plt.show()

test_generate_figure2()

在此处输入图像描述


推荐阅读