首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matplotlib绘图未显示所有xticks和yticks

Matplotlib绘图未显示所有xticks和yticks
EN

Stack Overflow用户
提问于 2021-09-21 04:23:44
回答 1查看 56关注 0票数 0

我在matplotlib中创建了子图,但并不是所有的xticks和yticks都被显示出来。我已经尝试了一切,从设置xlim和ylim,链式图形大小等。事情是这样的,这是hackerrnak上的一把手,他们正在根据他们的预期输出评估我的输出。xaxis中的0.0和yaxis上的1.0根本不匹配。我到底做错了什么。

这是代码,

代码语言:javascript
复制
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()

我的输出,

预期输出,

EN

回答 1

Stack Overflow用户

发布于 2021-09-21 05:11:46

你的set_xlim & set_ylim方法行得通。你只需要为每个子图设置它:

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

代码语言:javascript
复制
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()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69263352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档