Python Pandas 简明教程
Python Pandas - Visualization
Basic Plotting: plot
Series 和 DataFrame 上的此功能只是对 matplotlib libraries plot() 方法的简单包装。
This functionality on Series and DataFrame is just a simple wrapper around the matplotlib libraries plot() method.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range('1/1/2000',
periods=10), columns=list('ABCD'))
df.plot()
它的 output 如下所示 −
Its output is as follows −
如果索引包含日期,它将调用 gct().autofmt_xdate() 将 x 轴格式化为如上图所示。
If the index consists of dates, it calls gct().autofmt_xdate() to format the x-axis as shown in the above illustration.
我们可以使用 x 和 y 关键字将一列与另一列作对比。
We can plot one column versus another using the x and y keywords.
绘图方法允许一些绘图样式,这些样式与默认的线图不同。这些方法可以作为 plot() 的 kind 关键字参数提供。它们包括 -
Plotting methods allow a handful of plot styles other than the default line plot. These methods can be provided as the kind keyword argument to plot(). These include −
-
bar or barh for bar plots
-
hist for histogram
-
box for boxplot
-
'area' for area plots
-
'scatter' for scatter plots
Bar Plot
让我们通过创建一个条形图来看一个条形图是什么。条形图可以通过以下方式创建 -
Let us now see what a Bar Plot is by creating one. A bar plot can be created in the following way −
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.bar()
它的 output 如下所示 −
Its output is as follows −
若要制作堆叠条形图,使用 pass stacked=True −
To produce a stacked bar plot, pass stacked=True −
import pandas as pd
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.bar(stacked=True)
它的 output 如下所示 −
Its output is as follows −
若要获得水平条形图,使用 barh 方法 −
To get horizontal bar plots, use the barh method −
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.barh(stacked=True)
它的 output 如下所示 −
Its output is as follows −
Histograms
可以使用 plot.hist() 方法绘制直方图。我们可以指定柱的数量。
Histograms can be plotted using the plot.hist() method. We can specify number of bins.
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df.plot.hist(bins=20)
它的 output 如下所示 −
Its output is as follows −
若要针对每一列绘制不同的直方图,使用以下代码 −
To plot different histograms for each column, use the following code −
import pandas as pd
import numpy as np
df=pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df.diff.hist(bins=20)
它的 output 如下所示 −
Its output is as follows −
Box Plots
可以通过调用 Series.box.plot() 和 DataFrame.box.plot() 或 DataFrame.boxplot() 绘制箱形图,以可视化每列中的值分布。
Boxplot can be drawn calling Series.box.plot() and DataFrame.box.plot(), or DataFrame.boxplot() to visualize the distribution of values within each column.
例如,这里是一个箱线图,表示在 [0,1) 上的均匀随机变量的 10 次观测的五次试验。
For instance, here is a boxplot representing five trials of 10 observations of a uniform random variable on [0,1).
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()
它的 output 如下所示 −
Its output is as follows −
Area Plot
可以使用 Series.plot.area() 或 DataFrame.plot.area() 方法创建面积图。
Area plot can be created using the Series.plot.area() or the DataFrame.plot.area() methods.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area()
它的 output 如下所示 −
Its output is as follows −
Scatter Plot
可以使用 DataFrame.plot.scatter() 方法创建散点图。
Scatter plot can be created using the DataFrame.plot.scatter() methods.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b')
它的 output 如下所示 −
Its output is as follows −
Pie Chart
可以使用 DataFrame.plot.pie() 方法创建饼状图。
Pie chart can be created using the DataFrame.plot.pie() method.
import pandas as pd
import numpy as np
df = pd.DataFrame(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], columns=['x'])
df.plot.pie(subplots=True)
它的 output 如下所示 −
Its output is as follows −