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

    Customization

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

    • Labels

    It's time to customize your own plot. This is the fun part, you will see your plot come to life!

    # Basic scatter plot, log scale
    plt.scatter(gdp_cap, life_exp)
    plt.xscale('log')

    # Strings
    xlab = 'GDP per Capita [in USD]'
    ylab = 'Life Expectancy [in years]'
    title = 'World Development in 2007'

    # Add axis labels
    plt.xlabel(xlab)
    plt.ylabel(ylab)

    # Add title
    plt.title(title)

    # After customizing, display the plot
    plt.show()

    • Ticks

    Control the y-ticks by specifying two arguments:

    plt.yticks([0,1,2], ["one","two","three"])

    In this example, the ticks corresponding to the numbers 0, 1 and 2 will be replaced by onetwo and three, respectively.

    # Scatter plot
    plt.scatter(gdp_cap, life_exp)

    # Previous customizations
    plt.xscale('log')
    plt.xlabel('GDP per Capita [in USD]')
    plt.ylabel('Life Expectancy [in years]')
    plt.title('World Development in 2007')

    # Definition of tick_val and tick_lab
    tick_val = [1000,10000,100000]
    tick_lab = ['1k','10k','100k']

    # Adapt the ticks on the x-axis
    plt.xticks(tick_val,tick_lab)

    # After customizing, display the plot
    plt.show()

    • Sizes

    Right now, the scatter plot is just a cloud of blue dots, indistinguishable from each other. Let's change this. Wouldn't it be nice if the size of the dots corresponds to the population?

    To accomplish this, there is a list pop loaded in your workspace. It contains population numbers for each country expressed in millions. You can see that this list is added to the scatter method, as the argument s, for size.

    Instructions

    • Run the script to see how the plot changes.
    • Looks good, but increasing the size of the bubbles will make things stand out more.
      • Import the numpy package as np.
      • Use np.array() to create a numpy array from the list pop. Call this Numpy array np_pop.
      • Double the values in np_pop by assigningnp_pop * 2 to np_pop again. Becausenp_pop is a Numpy array, each array element will be doubled.
      • Change the s argument insideplt.scatter() to be np_pop instead ofpop.

    # Import numpy as np
    import numpy as np

    # Store pop as a numpy array: np_pop
    np_pop = np.array(pop)

    # Double np_pop
    np_pop = np_pop * 2

    # Update: set s argument to np_pop
    plt.scatter(gdp_cap, life_exp, s = np_pop)

    # Previous customizations
    plt.xscale('log')
    plt.xlabel('GDP per Capita [in USD]')
    plt.ylabel('Life Expectancy [in years]')
    plt.title('World Development in 2007')
    plt.xticks([1000, 10000, 100000],['1k', '10k', '100k'])

    # Display the plot
    plt.show()

    • Colors

    The code you've written up to now is available in the script on the right.

    The next step is making the plot more colorful! 

    # Specify c and alpha inside plt.scatter()
    plt.scatter(x = gdp_cap, y = life_exp, s = np.array(pop) * 2, c = col, alpha = 0.8)

    # Previous customizations
    plt.xscale('log')
    plt.xlabel('GDP per Capita [in USD]')
    plt.ylabel('Life Expectancy [in years]')
    plt.title('World Development in 2007')
    plt.xticks([1000,10000,100000], ['1k','10k','100k'])

    # Show the plot
    plt.show()

    • Additional Customizations

    If you have another look at the script, under # Additional Customizations, you'll see that there are two plt.text()functions now. They add the words "India" and "China" in the plot.

    # Scatter plot
    plt.scatter(x = gdp_cap, y = life_exp, s = np.array(pop) * 2, c = col, alpha = 0.8)

    # Previous customizations
    plt.xscale('log')
    plt.xlabel('GDP per Capita [in USD]')
    plt.ylabel('Life Expectancy [in years]')
    plt.title('World Development in 2007')
    plt.xticks([1000,10000,100000], ['1k','10k','100k'])

    # Additional customizations
    plt.text(1550, 71, 'India')
    plt.text(5700, 80, 'China')

    # Add grid() call
    plt.grid(True)

    # Show the plot
    plt.show()

  • 相关阅读:
    UVa 820 因特网带宽(最大流)
    UVa 1001 奶酪里的老鼠(Dijkstra或Floyd)
    UVa 821 网页跳跃(Floyd)
    UVa 11624 大火蔓延的迷宫
    UVa 10881 蚂蚁
    UVa 11300 分金币
    UVa 11729 突击战
    《额尔古纳河右岸》读书笔记
    HDU 1083 Courses(二分图匹配模板)
    UVa 10618 跳舞机
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7798169.html
Copyright © 2011-2022 走看看