diff --git a/sources/tech/20200602 Using pandas to plot data in Python.md b/sources/tech/20200602 Using pandas to plot data in Python.md deleted file mode 100644 index afa3d31192..0000000000 --- a/sources/tech/20200602 Using pandas to plot data in Python.md +++ /dev/null @@ -1,144 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: (geekpi) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (Using pandas to plot data in Python) -[#]: via: (https://opensource.com/article/20/6/pandas-python) -[#]: author: (Shaun Taylor-Morgan https://opensource.com/users/shaun-taylor-morgan) - -Using pandas to plot data in Python -====== -Pandas is a hugely popular Python data manipulation library. Learn how -to use its API to plot data. -![Two pandas sitting in bamboo][1] - -In this series of articles on Python-based plotting libraries, we're going to have a conceptual look at plots using pandas, the hugely popular Python data manipulation library. Pandas is a standard tool in Python for scalably transforming data, and it has also become a popular way to [import and export from CSV and Excel formats][2]. - -On top of all that, it also contains a very nice plotting API. This is extremely convenient—you already have your data in a pandas DataFrame, so why not use the same library to plot it? - -In this series, we'll be making the same multi-bar plot in each library so we can compare how they work. The data we'll use is UK election results from 1966 to 2020: - -![Matplotlib UK election results][3] - -### Data that plots itself - -Before we go further, note that you may need to tune your Python environment to get this code to run, including the following.  - - * Running a recent version of Python (instructions for [Linux][4], [Mac][5], and [Windows][6]) - * Verify you're running a version of Python that works with these libraries - - - -The data is available online and can be imported using pandas: - - -``` -import pandas as pd -df = pd.read_csv('') -``` - -Now we're ready to go. We've seen some impressively simple APIs in this series of articles, but pandas has to take the crown. - -To plot a bar plot with a group for each party and `year` on the x-axis, I simply need to do this: - - -``` -import matplotlib.pyplot as plt -    -ax = df.plot.bar(x='year') -    -plt.show() -``` - -Four lines—definitely the tersest multi-bar plot we've created in this series. - -I’m using my data in [wide form][7], meaning there’s one column per political party: - - -``` -        year  conservative  labour  liberal  others -0       1966           253     364       12       1 -1       1970           330     287        6       7 -2   Feb 1974           297     301       14      18 -..       ...           ...     ...      ...     ... -12      2015           330     232        8      80 -13      2017           317     262       12      59 -14      2019           365     202       11      72 -``` - -This means pandas automatically knows how I want my bars grouped, and if I wanted them grouped differently, pandas makes it easy to [restructure my DataFrame][8]. - -As with [Seaborn][9], pandas' plotting feature is an abstraction on top of Matplotlib, which is why you call Matplotlib's `plt.show()` function to actually produce the plot. - -Here's what it looks like: - -![pandas unstyled data plot][10] - -Looks great, especially considering how easy it was! Let's style it to look just like the [Matplotlib][11] example. - -#### Styling it - -We can easily tweak the styling by accessing the underlying Matplotlib methods. - -Firstly, we can color our bars by passing a Matplotlib colormap into the plotting function: - - -``` -from matplotlib.colors import ListedColormap -cmap = ListedColormap(['#0343df', '#e50000', '#ffff14', '#929591']) -ax = df.plot.bar(x='year', colormap=cmap) -``` - -And we can set up axis labels and titles using the return value of the plotting function—it's simply a [Matplotlib `Axis` object][12]. - - -``` -ax.set_xlabel(None) -ax.set_ylabel('Seats') -ax.set_title('UK election results') -``` - -Here's what it looks like now: - -![pandas styled plot][13] - -That's pretty much identical to the Matplotlib version shown above but in 8 lines of code rather than 16! My inner [code golfer][14] is very pleased. - -### Abstractions must be escapable - -As with Seaborn, the ability to drop down and access Matplotlib APIs to do the detailed tweaking was really helpful. This is a great example of giving an abstraction [escape hatches][15] to make it powerful as well as simple. - -* * * - -_This article is based on [How to make plots using Pandas][16] on Anvil's blog and is reused with permission._ - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/20/6/pandas-python - -作者:[Shaun Taylor-Morgan][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/shaun-taylor-morgan -[b]: https://github.com/lujun9972 -[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/panda.png?itok=0lJlct7O (Two pandas sitting in bamboo) -[2]: https://anvil.works/docs/data-tables/csv-and-excel -[3]: https://opensource.com/sites/default/files/uploads/matplotlib_2.png (Matplotlib UK election results) -[4]: https://opensource.com/article/20/4/install-python-linux -[5]: https://opensource.com/article/19/5/python-3-default-mac -[6]: https://opensource.com/article/19/8/how-install-python-windows -[7]: https://anvil.works/blog/tidy-data -[8]: https://anvil.works/blog/tidy-data#converting-between-long-and-wide-data-in-pandas -[9]: https://anvil.works/blog/plotting-in-seaborn -[10]: https://opensource.com/sites/default/files/uploads/pandas-unstyled.png (pandas unstyled data plot) -[11]: https://opensource.com/article/20/5/matplotlib-python -[12]: https://matplotlib.org/api/axis_api.html#axis-objects -[13]: https://opensource.com/sites/default/files/uploads/pandas_3.png (pandas styled plot) -[14]: https://en.wikipedia.org/wiki/Code_golf -[15]: https://anvil.works/blog/escape-hatches-and-ejector-seats -[16]: https://anvil.works/blog/plotting-in-pandas diff --git a/translated/tech/20200602 Using pandas to plot data in Python.md b/translated/tech/20200602 Using pandas to plot data in Python.md new file mode 100644 index 0000000000..d514b687e5 --- /dev/null +++ b/translated/tech/20200602 Using pandas to plot data in Python.md @@ -0,0 +1,140 @@ +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Using pandas to plot data in Python) +[#]: via: (https://opensource.com/article/20/6/pandas-python) +[#]: author: (Shaun Taylor-Morgan https://opensource.com/users/shaun-taylor-morgan) + +使用 pandas 在 Python 中绘制数据 +====== +Pandas 是一个非常流行的 Python 数据操作库。学习怎样使用它的 API 绘制数据。 +![Two pandas sitting in bamboo][1] + +在有关基于 Python 的绘图库的系列文章中,我们将对使用 pandas(一种非常流行的 Python 数据操作库)绘图进行概念性研究。Pandas 是 Python 中用于可扩展转换数据的标准工具,它也已成为[从 CSV 和 Excel 格式导入和导出数据][2]的流行方法。 + +最重要的是,它还包含一个非常好的绘图 API。这非常方便,你已将数据存储在 pandas DataFrame 中,那么为什么不使用相同的库进行绘制呢? + +在本系列中,我们将在每个库中制作相同的多柱状图,以便我们可以比较它们的工作方式。我们将使用的数据是 1966 年至 2020 年的英国大选结果: + +![Matplotlib UK election results][3] + +### 自行绘制的数据 + +在继续之前,请注意你可能需要调整 Python 环境来运行此代码,包括: + +* 运行最新版本的 Python([Linux][4]、[Mac][5] 和 [Windows][6] 的说明) +* 确认你运行的是与这些库兼容的 Python 版本 + + + +数据可在线获得,并可使用 pandas 导入: + +``` +import pandas as pd +df = pd.read_csv('') +``` + +完成了。在本系列文章中,我们已经看到了一些令人印象深刻的简单 API,但是 pandas 一定能夺冠。 + +要在 x 轴上绘制按`年`和每个党派分组的柱状图,我只需要这样做: + + +``` +import matplotlib.pyplot as plt +ax = df.plot.bar(x='year') +plt.show() +``` + +只有四行,这绝对是我们在本系列中创建的最棒的多柱状图。 + +我以[宽格式][7]使用数据,这意味着每个党派都有一列: + + +``` +year conservative labour liberal others +0 1966 253 364 12 1 +1 1970 330 287 6 7 +2 Feb 1974 297 301 14 18 +.. ... ... ... ... ... +12 2015 330 232 8 80 +13 2017 317 262 12 59 +14 2019 365 202 11 72 +``` + +这意味着 pandas 会自动知道我希望如何分组,如果我希望进行不同的分组,pandas 可以很容易地[重组 DataFrame][8]。 + +与 [Seaborn][9] 一样,pandas 的绘图功能是 Matplotlib 之上的抽象,这就是为什么要调用 Matplotlib 的 `plt.show()` 函数来实际生成绘图的原因。 + +看起来是这样的: + +![pandas unstyled data plot][10] + +看起来很棒,特别是它又这么简单!让我们对它进行样式设置,使其看起来像 [Matplotlib][11] 的例子。 + +#### 调整样式 + +我们可以通过访问底层的 Matplotlib 方法轻松地调整样式。 + +首先,我们可以通过将 Matplotlib 颜色表传递到绘图函数来为柱状图着色: + + +``` +from matplotlib.colors import ListedColormap +cmap = ListedColormap(['#0343df', '#e50000', '#ffff14', '#929591']) +ax = df.plot.bar(x='year', colormap=cmap) +``` + +我们可以使用绘图函数的返回值设置坐标轴标签和标题,它只是一个 [Matplotlib `Axis` 对象][12]。 + + +``` +ax.set_xlabel(None) +ax.set_ylabel('Seats') +ax.set_title('UK election results') +``` + +这是现在的样子: + +![pandas styled plot][13] + +这与上面的 Matplotlib 版本几乎相同,但是只用了 8 行代码而不是 16 行!我内心的[代码高尔夫选手][14]非常高兴。 + +### 抽象必须是可转义的 + +与 Seaborn 一样,向下访问 Matplotlib API 进行细节调整的能力确实很有帮助。这是给出抽象[紧急出口][15]使其既强大又简单的一个很好的例子。 + +* * * + +_本文基于 Anvil 博客上的[如何使用 Pandas 绘图][16],并获许可以重复使用。_ + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/6/pandas-python + +作者:[Shaun Taylor-Morgan][a] +选题:[lujun9972][b] +译者:[geekpi](https://github.com/geekpi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/shaun-taylor-morgan +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/panda.png?itok=0lJlct7O (Two pandas sitting in bamboo) +[2]: https://anvil.works/docs/data-tables/csv-and-excel +[3]: https://opensource.com/sites/default/files/uploads/matplotlib_2.png (Matplotlib UK election results) +[4]: https://opensource.com/article/20/4/install-python-linux +[5]: https://opensource.com/article/19/5/python-3-default-mac +[6]: https://opensource.com/article/19/8/how-install-python-windows +[7]: https://anvil.works/blog/tidy-data +[8]: https://anvil.works/blog/tidy-data#converting-between-long-and-wide-data-in-pandas +[9]: https://anvil.works/blog/plotting-in-seaborn +[10]: https://opensource.com/sites/default/files/uploads/pandas-unstyled.png (pandas unstyled data plot) +[11]: https://opensource.com/article/20/5/matplotlib-python +[12]: https://matplotlib.org/api/axis_api.html#axis-objects +[13]: https://opensource.com/sites/default/files/uploads/pandas_3.png (pandas styled plot) +[14]: https://en.wikipedia.org/wiki/Code_golf +[15]: https://anvil.works/blog/escape-hatches-and-ejector-seats +[16]: https://anvil.works/blog/plotting-in-pandas \ No newline at end of file