English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Pandas Visualization Operation Example
The functionality on Series and DataFrame is just a simple wrapping around the plot() method of the matplotlib library.
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(10,4)1/1/2000', periods=10) df.plot()
The running results are as follows:
If the index is composed of dates, it will call gct().autofmt_xdate() to format the x-axis, as shown in the figure above.
We can use the x and y keywords to draw the relationship between one column and another.
In addition to the default line chart, plotting methods also allow the use of various plotting styles. These methods can be provided as the kind keyword parameter of plot(). These include:
Bar Chart Histogram Boxplot Area Chart Scatter Plot Pie Chart
Below we will see how to create a bar chart:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(10,4) df.plot.bar()
The running results are as follows:
It produces a stacked bar chart, which can be set stacked=True
import pandas as pd df = pd.DataFrame(np.random.rand(10,4) df.plot.bar(stacked=True)
The running results are as follows:
To get a horizontal bar chart, you can use the barh method:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(10,4) df.plot.barh(stacked=True)
The running results are as follows:
You can use the plot.hist() method to plot histograms. We can specify the number.
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)
The running results are as follows:
You can use the following code to plot different histograms for each column:
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)
The running results are as follows:
Boxplot can be plotted by calling Series.box.plot() and DataFrame.box.plot() or DataFrame.boxplot() to visualize the distribution of values in each column.
For example, this is a boxplot representing the1For a random variable on10fifth test for a single observation.
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()
The running results are as follows:
You can create an area chart using Series.plot.area() or 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()
The running results are as follows:
You can create a scatter plot using the DataFrame.plot.scatter() method.
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')
The running results are as follows:
You can create a pie chart 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)
The running results are as follows: