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. Thymeleaf 入门

    基本项目结构: Thymeleaf配置: spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf ...

  2. [转]Android Path里FillType功能

    对于简单的封闭图形(路径无相交的现象),图形的外部和内部和容易判断,但如果路径有相交的情况,对应重叠的部分,使用不同的填充模式,内部和外部的定义有所不同.填充模式可以分为两类: EVEN_ODD 意味 ...

  3. 远程服务器git搭建

    在远程服务器如:/var/www下创建hello.git 然后git init --bare hello.git cd hello.git会看到下面的目录和文件 然后创建可以访问git的用户 git ...

  4. 8.29 jQuery

    2018-8-29 13:22:26 jQuery : http://www.cnblogs.com/liwenzhou/p/8178806.html 都快开学了!我得在家渡劫! 今天下午去俺弟家玩去 ...

  5. 【vue】---项目接口管理---【巷子】

    一.前言 在vue开发中,会涉及到很多接口的处理,当项目足够大时,就需要定义规范统一的接口 假设后端的文档分成了以下几个模块 1.发现模块 2.个人信息模块 3.商品模块 4.评论模块 ...... ...

  6. SequenceFile实例操作

    HDFS API提供了一种二进制文件支持,直接将<key,value>对序列化到文件中,该文件格式是不能直接查看的,可以通过hadoop  dfs -text命令查看,后面跟上Sequen ...

  7. tkinter 提示符

    在python3.4中,原来的tkMessageBox变成tkinter.messagebox,使用方式如下: import tkinter.messagebox tkinter.messagebox ...

  8. HDU - 5651 xiaoxin juju needs help 逆元模板

    http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...

  9. https-->http and http-->https bitransfer

    openssl s_client -connect myupload.mysite.net:443/cgi-bin/posupload.cgi -status -cert client.pem -ve ...

  10. /proc/meminfo

    /proc/meminfo  可以查看自己服务器 物理内存 注意这个文件显示的单位是kB而不是KB,1kB=1000B,但是实际上应该是KB,1KB=1024B 这个显示是不精确的,是一个已知的没有被 ...