ggplot简介
ggplot是一个Python绘图包,它基于R语言的ggplot2包和图形语法。ggplot与其他绘图包的关键区别是它的语法将数据与实际绘图明确地分离开来。为了对数据进行可视化表示,ggplot提供了几种基本元素:几何对象、图形属性和标度。除此之外,为了进行更高级的绘图,ggplot还提供一些附加元素:统计变换、坐标系、子窗口和可视化主题。
Python的ggplot库不像R语言的ggplot2库那样成熟,所以它不具备ggplot2的所有功能。也就是说,它没有那么多的几何对象、统计变换和标度,也没有坐标系、注释和增强功能。在与ggplot相关的包进行了升级与修改之后,使用ggplot也可能会遇到问题。
安装ggplot模块
打开Anaconda Prompt(如果你没有安装Anaconda的话,打开命令行窗口即可),输入命令pip install ggplot
,等待安装完成。
参考书的示例代码及问题
#!/usr/bin/env python3
from ggplot import *
print(mtcars.head())
plt1 = ggplot(aes(x='mpg'), data=mtcars) +\
geom_histogram(fill='darkblue', binwidth=2) +\
xlim(10, 35) + ylim(0, 10) +\
xlab("MPG") + ylab("Frequency") +\
ggtitle("Histogram of MPG") +\
theme_matplotlib()
print(plt1)
print(meat.head())
plt2 = ggplot(aes(x='date', y='beef'), data=meat) +\
geom_line(color='purple', size=1.5, alpha=0.75) +\
stat_smooth(colour='blue', size=2.0, span=0.15) +\
xlab("Year") + ylab("Head of Cattle Slaughtered") +\
ggtitle("Beef Consumption Over Time") +\
theme_seaborn()
print(plt2)
print(diamonds.head())
plt3 = ggplot(diamonds, aes(x='carat', y='price', colour='cut')) +\
geom_point(alpha=0.5) +\
scale_color_gradient(low='#05D9F6', high='#5011D1') +\
xlim(0, 6) + ylim(0, 20000) +\
xlab("Carat") + ylab("Price") +\
ggtitle("Diamond Price by Carat and Cut") +\
theme_gray()
print(plt3)
ggsave(plt3, "ggplot_plots.png")
我在运行这个脚本的时候,PyCharm报错信息为:AttributeError: module 'pandas' has no attribute 'tslib'
。我当时就懵了:这是什么情况???
后来我在网上查找了一下相关资料,以下是对这个问题的解答。
抱歉,看起来ggplot(又称ggpy)是一个官方不再维护的项目了,github上的最近一次提交还是三年前。而
AttributeError: module 'pandas' has no attribute 'tslib'
也是已知的由于 pandas 版本更新,API改变带来的bug。
解答者还在回答中提供了ggplot模块在GitHub上的issue和PR,但是我英语水平一般,就没有仔细地阅读这些,而是打算自己动手解决。
解决方案
我们找到smoothers.py和utils.py这两个文件,如下图所示修改红框中的内容。
Tips:
smoothers.py的路径:D:\Anaconda3\Lib\site-packages\ggplot\stats\smoothers.py
utils.py的路径:D:\Anaconda3\Lib\site-packages\ggplot\utils.py
由于Anaconda3文件夹保存位置不同,路径可能会有差异。
修改完成之后,我们再次运行上面的代码。
运行结果显示报错信息:NameError: name 'theme_matplotlib' is not defined
。此时我的内心是崩溃的:我的天,怎么还是有问题啊!!!
好在我很快地冷静了下来,开始着手分析这个问题。
我打开了ggplot.py文件,按下Ctrl+F组合键,输入“theme”进行搜索,得到如下图所示的搜索信息。
Tips:
ggplot.py的路径:D:\Anaconda3\Lib\site-packages\ggplot\ggplot.py
由于Anaconda3文件夹保存位置不同,路径可能会有差异。
接下来,我打开了ggplot文件夹中的themes文件夹,发现……emmmm……它好像根本就没有theme_matplotlib
和theme_seaborn
啊!!!
我的内心再一次崩溃。
算了,没有就没有吧,大不了我不用就是了。
于是,经过了一番折腾,代码变成了下面这个样子:
#!/usr/bin/env python3
from ggplot import *
print(diamonds.head())
plt = ggplot(diamonds, aes(x='carat', y='price', colour='cut')) + \
geom_point(alpha=0.5) + \
scale_color_gradient(low='#05D9F6', high='#5011D1') + \
xlim(0, 6) + ylim(0, 20000) + \
xlab("Carat") + ylab("Price") + \
ggtitle("Diamond Price by Carat and Cut") + \
theme_gray()
print(plt)
ggsave(plt, "ggplot_plots.png")
再运行一次。你们猜怎么样?
又报错了。
好在之前已经折腾了那么多,再报错我也见怪不怪了。我再次打开ggplot.py文件,以“save”为关键字搜索一下。
原来它不叫ggsave
而是叫save
啊。这样一来就好办多了,我们再修改一下代码:
#!/usr/bin/env python3
from ggplot import *
print(diamonds.head())
plt = ggplot(diamonds, aes(x='carat', y='price', colour='cut')) + \
geom_point(alpha=0.5) + \
scale_color_gradient(low='#05D9F6', high='#5011D1') + \
xlim(0, 6) + ylim(0, 20000) + \
xlab("Carat") + ylab("Price") + \
ggtitle("Diamond Price by Carat and Cut") + \
theme_gray()
print(plt)
ggplot.save(plt, "ggplot_plots.png", dpi=400)
这次,它终于没有报错!没有报错!没有报错!!!
不过它保存出来的这个png格式文件……红框里面的内容不太一样,图上的点的颜色也有些许不同……这我就不是很清楚了。
写在最后
我觉得,正是由于Python的ggplot模块没有R语言的ggplot2模块那么成熟,再加上官方已经有三年多的时间没有对其进行维护和升级了,这期间它所依赖的模块有所更新(比如pandas),才会导致这一系列问题。所以,如果想使用Python进行统计图表的绘制,相较于ggplot模块,matplotlib和seaborn会更加友好一些。