1

Problem: Unable to identify why the xticks and yticks are missing few values from the list provided My code is as follows:

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

My output is: enter image description here

My desired output is: enter image description here

4 Answers4

1

Setting the ticks does not automatically update the axis limits, you should instead set them explicitly:

axes4.set_xlim(0,1.2)
Andrea
  • 2,932
  • 11
  • 23
0

You can manually set the axis with

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

etc.

mrtnlrsn
  • 1,105
  • 11
  • 19
0

##Remove xticks and use "xlim" and "ylim" within "plt.subplot()".

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

This code will set the limits and it will be align with the expected values.

Josef
  • 2,869
  • 2
  • 22
  • 23
P K M
  • 51
  • 6