【问题标题】:xticks and yticks not displaying few values in scatterplot using matplotlibxticks 和 yticks 使用 matplotlib 在散点图中没有显示很少的值
【发布时间】:2020-04-21 19:58:46
【问题描述】:

问题: 无法确定为什么 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()

我的输出是:

我想要的输出是:

【问题讨论】:

    标签: python matplotlib scatter-plot xticks


    【解决方案1】:

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

    axes4.set_xlim(0,1.2)
    

    【讨论】:

      【解决方案2】:

      您可以手动设置轴

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

      等等

      【讨论】:

        【解决方案3】:

        ##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()
        

        【讨论】:

          【解决方案4】:
          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)
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-11-18
            • 2019-11-29
            • 1970-01-01
            • 2021-07-08
            • 2021-10-07
            • 1970-01-01
            • 2021-11-11
            • 2018-08-22
            相关资源
            最近更新 更多