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

Intermediate Python for Data Science learning 3 - Customization的更多相关文章

  1. Intermediate Python for Data Science learning 2 - Histograms

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

  2. Intermediate Python for Data Science learning 1 - Basic plots with matplotlib

    Basic plots with matplotlib from:https://campus.datacamp.com/courses/intermediate-python-for-data-sc ...

  3. 学习笔记之Intermediate Python for Data Science | DataCamp

    Intermediate Python for Data Science | DataCamp https://www.datacamp.com/courses/intermediate-python ...

  4. Intro to Python for Data Science Learning 8 - NumPy: Basic Statistics

    NumPy: Basic Statistics from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/ch ...

  5. Intro to Python for Data Science Learning 7 - 2D NumPy Arrays

    2D NumPy Arrays from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4- ...

  6. Intro to Python for Data Science Learning 5 - Packages

    Packages From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-functio ...

  7. Intro to Python for Data Science Learning 2 - List

    List from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-2-python-list ...

  8. Intro to Python for Data Science Learning 6 - NumPy

    NumPy From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=1 ...

  9. Intro to Python for Data Science Learning 4 - Methods

    Methods From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-function ...

随机推荐

  1. dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/sudo) is setuid or setgid

    这两个变量被设置了 DYLD_LIBRARY_PATH *或* LD_LIBRARY_PATH, 用下面的明令查找一下,一般在.bash_profile, .bashrc 等文件中.执行brew do ...

  2. Java虚拟机九 java.lang.String在虚拟机中的实现

    在Java中,Java的设计者对String对象进行了大量的优化,主要有三个特点: 1.不变性: 不变性是指String对象一旦生成,则不能再对它进行改变.String的这个特点可以泛化成不变(imm ...

  3. python中filter(),map()和reduce()的用法及区别

    先看filter()方法 print(list(filter(lambda n : n % 2 == 1, range(20))))# 结果 [1, 3, 5, 7, 9, 11, 13, 15, 1 ...

  4. MatLab Swap Rows or Cols 交换行或列

    Matlab是矩阵运算的神器,所以可以很轻易的交换任意行或列,而且写法非常简洁,如下所示: a = [ ; ; ]; b = a; b(:,[;]) = b(:,[;]); % Swap col an ...

  5. codeforces#514 Div2---1059ABCD

    1059A---Cashier http://codeforces.com/contest/1059/problem/A 题意: Vasya每天工作\(l\)个小时,每天服务\(n\)个顾客,每个休息 ...

  6. BAT等大厂已开源的70个实用工具盘点(附下载地址)

    前面的一篇文章<微软.谷歌.亚马逊.Facebook等硅谷大厂91个开源软件盘点(附下载地址)>列举了国外8个互联网公司(包括微软.Google.亚马逊.IBM.Facebook.Twit ...

  7. cocoapods卸载与安装

    引用自:https://www.aliyun.com/jiaocheng/389907.html 一.首先卸载pod which pod 得到pod的路径 sudo rm -rf <pod的路径 ...

  8. ndk http://www.th7.cn/Program/Android/201412/334955.shtml

    http://www.th7.cn/Program/Android/201412/334955.shtml http://ruikye.com/2014/08/30/androidstudio_ndk ...

  9. UITableView左右滑动cell无法显示“删除”按钮的原因分析

    http://www.cocoachina.com/bbs/read.php?tid-145693.html - (void)tableView:(UITableView *)tableView co ...

  10. LightOJ 1027 - A Dangerous Maze(求期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...