zoukankan      html  css  js  c++  java
  • Intermediate Python for Data Science learning 1

    Basic plots with matplotlib

    from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib?ex=1

    • Line plot (1)

    With matplotlib, you can create a bunch of different plots in Python. The most basic plot is the line plot. A general recipe is given here.

    import matplotlib.pyplot as plt
    plt.plot(x,y)
    plt.show()

    # Print the last item from year and pop
    print(year[-1])
    print(pop[-1])

    # Import matplotlib.pyplot as plt
    import matplotlib.pyplot as plt

    # Make a line plot: year on the x-axis, pop on the y-axis
    plt.plot(year,pop)

    # Display the plot with plt.show()
    plt.show()

    • Line Plot (2): Interpretation

    Have another look at the plot you created in the previous exercise; it's shown on the right. Based on the plot, in approximately what year will there be more than ten billion human beings on this planet?

    pop[year.index(2060)]

    You can check the population for a particular year by checking out the plot. If you want the exact result, use pop[year.index(2030)], to get the population for 2030, for example.

    • Line plot (3)

    Now that you've built your first line plot, let's start working on the data that professor Hans Rosling used to build his beautiful bubble chart. It was collected in 2007. Two lists are available for you:

    • life_exp which contains the life expectancy for each country and
    • gdp_cap, which contains the GDP per capita (i.e. per person) for each country expressed in US Dollars.

    # Print the last item of gdp_cap and life_exp
    print(gdp_cap[-1])
    print(life_exp[-1])

    # Make a line plot, gdp_cap on the x-axis, life_exp on the y-axis
    import matplotlib.pyplot as plt
    plt.plot(gdp_cap,life_exp)

    # Display the plot
    plt.show()

    • Scatter Plot (1)

    When you have a time scale along the horizontal axis, the line plot is your friend. But in many other cases, when you're trying to assess if there's a correlation between two variables, for example, the scatter plot is the better choice. Below is an example of how to build a scatter plot.

    import matplotlib.pyplot as plt
    plt.scatter(x,y)
    plt.show()

    # Change the line plot below to a scatter plot
    plt.scatter(gdp_cap, life_exp)

    # Put the x-axis on a logarithmic scale. A correlation will become clear when you display the GDP per capita on a logarithmic scale. Add the line plt.xscale('log')
    plt.xscale('log')

    # Show plot
    plt.show()

    • Scatter plot (2)

    In the previous exercise, you saw that that the higher GDP usually corresponds to a higher life expectancy. In other words, there is a positive correlation.

    Do you think there's a relationship between population and life expectancy of a country? The list life_exp from the previous exercise is already available. In addition, now also pop is available, listing the corresponding populations for the countries in 2007. The populations are in millions of people.

    # Import package
    import matplotlib.pyplot as plt

    # Build Scatter plot
    plt.scatter(pop,life_exp)

    # Show plot
    plt.show()

  • 相关阅读:
    fluent/starccm/商业CFD软件中残差的概念
    windows 下用命令来操作定时任务
    selenium下打开Chrome报错解决
    TypeError: a bytes-like object is required, not 'str'
    Cannot redeclare class phpmailerException
    linux freetds无法构建错误:为--with-tdsver:8.0指定的值无效
    Apache 修改端口号
    php 二维数组按照某个键排序
    php 计算 距离
    pymysql 读取数据库没有字段
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7794258.html
Copyright © 2011-2022 走看看