xticks和yticks在使用matplotlib绘制散点图时没有显示一些值。

huangapple go评论91阅读模式
英文:

xticks and yticks not displaying few values in scatterplot using matplotlib

问题

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

我的输出是:
xticks和yticks在使用matplotlib绘制散点图时没有显示一些值。

我的期望输出是:
xticks和yticks在使用matplotlib绘制散点图时没有显示一些值。

英文:

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:
xticks和yticks在使用matplotlib绘制散点图时没有显示一些值。

My desired output is:
xticks和yticks在使用matplotlib绘制散点图时没有显示一些值。

答案1

得分: 1

设置刻度并不会自动更新坐标轴的限制,你应该明确地设置它们:

axes4.set_xlim(0,1.2)
英文:

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

axes4.set_xlim(0,1.2)

答案2

得分: 0

你可以通过以下方式手动设置坐标轴:

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

等等。

英文:

You can manually set the axis with

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

etc.

答案3

得分: 0

fig = plt.figure(figsize=(12, 10))
axes1 = plt.subplot(2, 2, 1, title="散点图与上三角标记", xlim=(0, 1.2), ylim=(-0.2, 1.0))
axes2 = plt.subplot(2, 2, 2, title="散点图与加号标记", xlim=(0, 1.2), ylim=(-0.2, 1.0))
axes3 = plt.subplot(2, 2, 3, title="散点图与圆圈标记", xlim=(0, 1.2), ylim=(-0.2, 1.0))
axes4 = plt.subplot(2, 2, 4, title="散点图与菱形标记", xlim=(0, 1.2), ylim=(-0.2, 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()
英文:

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

答案4

得分: 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)
英文:
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.

huangapple
  • 本文由 发表于 2020年1月3日 16:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定