简体   繁体   English

python 中 plot 的 Y 轴倾斜

[英]Y-Axis of plot in python skewed

I am new to Python and I've been trying to create a program that plots a lorenz curve.我是 Python 的新手,我一直在尝试创建一个绘制洛伦兹曲线的程序。 When I don't define the function it works fine but when I do, the numbers still work out but the plot looks wonky.当我没有定义 function 时,它工作正常,但当我定义时,数字仍然有效,但 plot 看起来很不稳定。 I can't figure out how to fix the y-axis- My range is 0-100, and I'd like intervals of 10 or 20 but the program smushes all the y-axis values together.我不知道如何修复 y 轴 - 我的范围是 0-100,我想要 10 或 20 的间隔,但程序会将所有 y 轴值混合在一起。 I've tried everything I've found on the internet so far but nothing worked.到目前为止,我已经尝试了我在互联网上找到的所有内容,但没有任何效果。 Here's my code:这是我的代码:

def lorenz():
    import matplotlib.pyplot as plt
    import numpy as np

    print("Enter distribution per quintile from poorest to richest one by one. Entering the starting 0 value is not necessary. Make sure they add up to 100%")
    T1=input("Distribution for bottom 20%: ")
    T2=input("Distribution for next 20%: ")
    T3=input("Distribution for next 20%: ")
    T4=input("Distribution for next 20%: ")
    T5=input("Distribution for top 20%: ")

    Q1=T1
    Q2=Q1+T2
    Q3=Q2+T3
    Q4=Q3+T4
    Q5=100

    x1=(0,20,40,60,80,100)
    y1=(0,Q1,Q2,Q3,Q4,Q5)
    x2=(0,100)
    y2=(0,100)
    x3=(0,100)
    y3=(0,0)

    plt.plot(x1, y1,'-o', label="Country X")
    plt.plot(x2, y2, 'g' ,label="Perfect Equality")
    plt.plot(x3, y3, 'r',label="Perfect Inequality")
    plt.ylim(0,100)


    plt.title('Lorenz Curve of Country X')
    plt.xlabel('% of Population')
    plt.ylabel('% of Income')
    plt.legend()
    plt.legend(loc='upper left')
    plt.show()

Here's how it ends up looking这是它最终的样子

在此处输入图像描述

The reason your plots are skewed is because inputs are being read as strings and not integers.您的绘图偏斜的原因是因为输入被读取为字符串而不是整数。 Convert them to integers and it will work.将它们转换为整数,它将起作用。 Below are my adjustments:以下是我的调整:

import matplotlib.pyplot as pat
import numpy as np

def lorenz():
  print("Enter distribution per quintile from poorest to richest one by one. Entering the starting 0 value is not necessary. Make sure they add up to 100%")
  T1=input("Distribution for bottom 20%: ")
  T2=input("Distribution for next 20%: ")
  T3=input("Distribution for next 20%: ")
  T4=input("Distribution for next 20%: ")
  T5=input("Distribution for top 20%: ")

  Q1=int(T1)
  Q2=int(Q1)+int(T2)
  Q3=int(Q2)+int(T3)
  Q4=int(Q3)+int(T4)
  Q5=100

  x1=(0,20,40,60,80,100)
  y1=(0,Q1,Q2,Q3,Q4,Q5)
  x2=np.linspace(0,100, num=6)
  y2=np.linspace(0,100, num=6)
  x3=np.linspace(0,100, num=6)
  y3=np.zeros((6, 1))

  lorenz_fig = plt.figure(figsize=[10,3])
  lorenz_ax = lorenz_fig.add_axes([0, 0, 1, 1], label=['Country X', 
                                                        'Perfect Equality',
                                                        'Perfect Inequality'])
  lorenz_ax.plot(x1, y1,'-o', x2, y2, 'g', x3, y3, 'r')

  plt.title('Lorenz Curve of Country X')
  plt.xlabel('% of Population')
  plt.ylabel('% of Income')
  lorenz_fig.legend(lorenz_ax.lines, ['Country X', 'Perfect Equality',
                               'Perfect Inequality'], 
                               fontsize='large', loc='upper left')

  plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM