作为一个ggplot2的死忠粉,迫于生计,开始学习matplotlib,这里是一些随想随记,Giao!我对matplotlib其实一点意见都没有,只是因为R是我系的官方语言,而且确实ggplot2的体验还是可以的,那一套东西也有严格的理论根据,厉害吧,一个有理论支持的画图工具。所以一直没尝试去理解过matplotlib的作图逻辑,现在嘛,因为要回国,感觉国内其实大家对R的使用率还是有点低,所以,随大流咯,学一波。
我是跟着DataCamp的课来学的,之后也会一直分析自己的笔记,上课的原版PPT也会分享在CSDN。
我这里安利一下DataCamp,虽然它完全没有给我钱,很多人觉得DataCamp的课程很智障,但是我真的觉得很棒很棒,特别是对于入门基础知识的同学,因为请的老师,绝大多数都是大牛,很多甚至是先驱级别的任务,更有很多API的原开发着,顺着他们的设计思路来学,会有容易,也能理解到精髓!
但是如果想学什么机器学习原理,或者是什么深度学习框架的,这个肯定是不适合的,就我个人感觉,DataCamp是绝对绝对特别适合数据分析师的!可能不是那么时候数据挖掘,数据工程师之类的岗位!
下面就是笔记提纲啦!
选API的原则就是,什么用的人多,那就用什么!所以一开始肯定是:
import matplotlib.pyplot as plt
然后就是我今天学到的最重要的一个东西!懂了这个关键的地方,之后的就触类旁通了!
fig, ax = plt.subplots()
# fig: Figure Object is a container that holds everything you see on the page
# ax: Axes is the part of the page that holds the data. It is the canvas on which we will draw with our data to visualize it.
之前看到有些博文教程一会儿plt, 一会儿ax的感觉乱的不得了,特别是ax这个东西,真的很迷,一直不知道是啥。现在其实说白了,就是个容器(Container), 在Python中自然是个类,它存贮了坐标系,做标轴,刻度等等。而我们画图所需要做的,其实就是把数据,以合适的方式/你想要的方式,放进ax里,然后画出来。
懂了这个之后,下面的几乎就全是语法了。
我通过截取代码片段来附加说明,可以不用在意代码中的数据。
fig, ax = plt.subplots()
# 1. 放数据
# 2. 设定格式: marker, linstyle, color...
ax.plot(seattle_weather["MONTH"], seattle_weather["MLY-TAVG-NORMAL"], marker="v", linestyle="--", color="r")
plt.show()
ax.set_xlabel("Time (months)")
ax.set_ylabel("Average temperature (Fahrenheit degrees)")
fig, ax = plt.subplots(m, n)
# 此时,fig, ax都变成相应维度(m, n)的List,
fig, ax = plt.subplots()
# .index熟悉pandas都知道是把作为index的那一列提取出来
ax.plot(climate_change.index, climate_change["co2"], color='blue')
ax.set_xlabel('Time') ax.set_ylabel('CO2 (ppm)', color='blue')
# 改变tick的颜色
ax.tick_params('y', colors='blue')
# 创建一个新的container, ax2来存放另外一组数据
# 此时,两个container共享x坐标轴,且会自动调整y轴尺寸和比例
ax2 = ax.twinx()
ax2.plot(climate_change.index,climate_change["relative_temp"], color='red')
ax2.set_ylabel('Relative temperature (Celsius)', color='red')
# 改变tick的颜色
ax2.tick_params('y', colors='red')
plt.show()
fig, ax = plt.subplots()
plot_timeseries(ax, climate_change.index, climate_change['co2'], 'blue', 'Time', 'CO2 (ppm)')
ax2 = ax.twinx() plot_timeseries(ax2, climate_change.index, climate_change['relative_temp'],'red', 'Time', 'Relative temperature (Celsius)')
# xy指定标注对象的位置
# xytext指定标注文本的位置
# arrowprops连接标注文本和标注对象
ax2.annotate(">1 degree",
xy=(pd.Timestamp('2015-10-06'), 1),
xytext=(pd.Timestamp('2008-10-06'), -0.2),
arrowprops={"arrowstyle":"->", "color":"gray"})
plt.show()
# Bar: 需要指定x坐标,y坐标本身和位置
# 可以一个一个Bar的加,下面的代码时一类一类的Bars在加
ax.bar(medals.index, medals["Gold"])
ax.bar(medals.index, medals["Silver"],bottom=medals["Gold"]) ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"])
# 这里还有errorbar之类的操作,但我平时用的也不多,就跳过了。
# Histogram
# 指定bin的个数
ax.hist(mens_rowing["Height"], label="Rowing", bins=5)
# 指定bin的大小
ax.hist(mens_rowing["Height"], label="Rowing", bins=[150, 160, 170, 180, 190, 200, 210])
# Boxplot: 需要在数据放入后单独指定x的坐标
ax.boxplot([mens_rowing["Height"],mens_gymnastics["Height"]])
ax.set_xticklabels(["Rowing", "Gymnastics"])
# Scatter
# 这里的c我感觉就是类的缩写,虽然表现出来时颜色,我觉得和ggplot里面的group很像
ax.scatter(climate_change["co2"], climate_change["relative_temp"], c=climate_change.index)
plt.style.use("ggplot")
# 回到默认值
plt.style.use("default")
plt.style.use("bmh")
plt.style.use("seaborn-colorblind")
fig.savefig("gold_medals.png", dpi=300)
fig.savefig("gold_medals.jpg", quality=50)
fig.set_size_inches([5, 3])
不论如何,我绝对之后至少我对matplotlib先天的恐惧就没有啦,再也不会稍微需要画图的时候就把数据导入Rstudio里面了啊哈哈哈哈哈!