Posted by Shen's Blog on Monday, January 1, 0001

4.图形绘制

图形可视化是非常强大的工具

Hans Rosling例子 hans

Matplot

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "last_expr"
import matplotlib.pyplot as plt  
year = [1950, 1970, 1990, 2010]      # 世界人口数据
pop = [2.519, 3.692, 5.263, 6.972] 
plt.plot(year, pop)                  # 折线图
plt.show()


plt.scatter(year, pop)               # 散点图
plt.show()

plt.plot(year, pop)
plt.xlabel("year")                # 不能使用Unicode, "年"
plt.ylabel("population")          # 坐标轴标注
plt.title("world population")     # 标题
plt.show()

plt.plot(year, pop)
plt.fill_between(year,pop,0,color='lightblue') # 填充色
plt.xlabel("year")
plt.ylabel("population")
plt.title("world population")
plt.yticks([0,2,4,6,8,10],
    ["0","2B","4B","6B","8B","10B"])
plt.show()

直方图

  • 数据分布的可视化
  • 数据分布

分布

直方图文档 histlink

histdoc

values = [0,0.6,1.4,1.6,2.2,2.5,2.6,3.2,3.5,3.9,4.2,6] 
plt.hist(values, bins=4)                                    # 分布区间数目
plt.show()
plt.hist(shmale_bmi, bins=20)     # 几乎是一个钟形曲线
plt.show()