官术网_书友最值得收藏!

Plotting functions with Pandas

We have covered most of the important components in a plot figure using matplotlib. In this section, we will introduce another powerful plotting method for directly creating standard visualization from Pandas data objects that are often used to manipulate data.

For Series or DataFrame objects in Pandas, most plotting types are supported, such as line, bar, box, histogram, and scatter plots, and pie charts. To select a plot type, we use the kind argument of the plot function. With no kind of plot specified, the plot function will generate a line style visualization by default , as in the following example:

>>> s = pd.Series(np.random.normal(10, 8, 20))
>>> s.plot(style='ko—', alpha=0.4, label='Series plotting')
>>> plt.legend()
>>> plt.show()

The output for the preceding command is as follows:

Another example will visualize the data of a DataFrame object consisting of multiple columns:

>>> data = {'Median_Age': [24.2, 26.4, 28.5, 30.3],
 'Density': [244, 256, 268, 279]}
>>> index_label = ['2000', '2005', '2010', '2014'];
>>> df1 = pd.DataFrame(data, index=index_label)
>>> df1.plot(kind='bar', subplots=True, sharex=True)
>>> plt.tight_layout();
>>> plt.show()

The output for the preceding command is as follows:

The plot method of the DataFrame has a number of options that allow us to handle the plotting of the columns. For example, in the above DataFrame visualization, we chose to plot the columns in separate subplots. The following table lists more options:

主站蜘蛛池模板: 红原县| 大洼县| 江西省| 上饶县| 伊宁市| 喀喇沁旗| 华宁县| 昂仁县| 琼中| 临安市| 阿鲁科尔沁旗| 鹿邑县| 杭锦旗| 韶山市| 苍梧县| 卫辉市| 嘉善县| 宁阳县| 绥化市| 开原市| 蓝田县| 泉州市| 纳雍县| 泗洪县| 屏山县| 大丰市| 平昌县| 隆昌县| 泊头市| 英山县| 瓮安县| 天峻县| 徐州市| 许昌县| 古浪县| 中宁县| 浮山县| 大悟县| 浠水县| 隆尧县| 大新县|