1

问题:无法确定 xticks 和 yticks 从提供的列表中缺少几个值的原因 我的代码如下:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

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(221, title='Scatter plot with Upper Traingle Markers')
axes1.set_xticks([0.0, 0.4, 0.8, 1.2])
axes1.set_yticks([-0.2, 0.2, 0.6, 1.0])
axes1.scatter(x,y,marker='^',s=[80],c=z,edgecolor='black')

axes2=plt.subplot(222, 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,edgecolor='black')

axes3=plt.subplot(223, 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,edgecolor='black')    

axes4=plt.subplot(224, 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,edgecolor='black')

plt.tight_layout()
plt.show()

我的输出是: 在此处输入图像描述

我想要的输出是: 在此处输入图像描述

4

4 回答 4

1

设置刻度不会自动更新轴限制,您应该明确设置它们:

axes4.set_xlim(0,1.2)
于 2020-01-03T08:46:37.937 回答
0

##Remove xticks 并在“plt.subplot()”中使用“xlim”和“ylim”。

    fig = plt.figure(figsize = (12,10))
    axes1 = plt.subplot(2,2,1,title = "Scatter plot with Upper Traingle 
    Markers",xlim= 0,1.2),ylim=(-0.2,1.0))
    #plt.xticks([0.0, 0.4, 0.8, 1.2])
    #plt.xticks(np.arange(-0.2, 1.2, step=0.4))
    #plt.yticks([-0.2, 0.2, 0.6, 1.0])
    axes2 = plt.subplot(2,2,2,title = "Scatter plot with Plus Markers",xlim= 
    (0,1.2),ylim=(-0.2,1.0))
   # plt.xticks([0.0, 0.4, 0.8, 1.2])
    # plt.yticks([-0.2, 0.2, 0.6, 1.0])
    axes3 = plt.subplot(2,2,3,title = "Scatter plot with Circle Markers",xlim= 
    (0,1.2),ylim=(-0.2,1.0))
    # plt.xticks([0.0, 0.4, 0.8, 1.2])
    # plt.yticks([-0.2, 0.2, 0.6, 1.0])
    axes4 = plt.subplot(2,2,4,title = "Scatter plot with Diamond Markers",xlim= 
    (0,1.2),ylim=(-0.2,1.0))
    # plt.xticks([0.0, 0.4, 0.8, 1.2])
    # plt.yticks([-0.2, 0.2, 0.6, 1.0])
    np.random.seed(1000)
    x = np.random.rand(10)
    y = np.random.rand(10)
    z = np.sqrt(x**2+y**2)
    axes1.scatter(x,y, s = 80,c =z,marker = "^")
    axes2.scatter(x,y, s = 80,c =z,marker = "+")
    axes3.scatter(x,y, s = 80,c =z,marker = "o")
    axes4.scatter(x,y, s = 80,c =z,marker = "d")
    plt.tight_layout()
    plt.show()
于 2020-12-29T23:24:40.010 回答
0
axes1.set_xlim(0, 1.2)
axes1.set_ylim(-0.2, 1.0)
axes2.set_xlim(0, 1.2)
axes2.set_ylim(-0.2, 1.0)
axes3.set_xlim(0, 1.2)
axes3.set_ylim(-0.2, 1.0)
axes4.set_xlim(0, 1.2)
axes4.set_ylim(-0.2, 1.0)

此代码将设置限制,并将与预期值保持一致。

于 2021-07-02T05:52:02.480 回答
0

您可以手动设置轴

axes1.axis([-0.1, 1.2, -0.2, 1.1])

等等

于 2020-01-03T08:49:39.320 回答