码迷,mamicode.com
首页 > 编程语言 > 详细

Python 笔记 #09# Basic plots with matplotlib

时间:2018-01-13 23:49:00      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:log   cap   lin   last   isp   basic   pop   title   width   

源:DataCamp

技术分享图片

气泡的大小表示人口的多少,横坐标表示人均GDP(美元),纵坐标表示预期寿命。-- 作者:Hans Rosling

 

Python 中有许许多多用于可视化的包,而 matplotlib 是它们的源头。

我们需要用到的是它的子包 pyplot ,通常它被简写成 plt 导入

1、Line plot 

技术分享图片

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

 \

技术分享图片

# 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
plt.plot(gdp_cap, life_exp)

# Display the plot
plt.show()

 

2、Scatter Plot 

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.

技术分享图片

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

# Put the x-axis on a logarithmic scale
plt.xscale(log)

# Show plot
plt.show()

 \

技术分享图片

# Import package
import matplotlib.pyplot as plt

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

# Show plot
plt.show()

 

Python 笔记 #09# Basic plots with matplotlib

标签:log   cap   lin   last   isp   basic   pop   title   width   

原文地址:https://www.cnblogs.com/xkxf/p/8280533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!