Visualization

We use the standard convention for referencing the matplotlib API:

  1. In [1]: import matplotlib.pyplot as plt
  2.  
  3. In [2]: plt.close('all')

We provide the basics in pandas to easily create decent looking plots.See the ecosystem section for visualizationlibraries that go beyond the basics documented here.

Note

All calls to np.random are seeded with 123456.

Basic plotting: plot

We will demonstrate the basics, see the cookbook forsome advanced strategies.

The plot method on Series and DataFrame is just a simple wrapper aroundplt.plot():

  1. In [3]: ts = pd.Series(np.random.randn(1000),
  2. ...: index=pd.date_range('1/1/2000', periods=1000))
  3. ...:
  4.  
  5. In [4]: ts = ts.cumsum()
  6.  
  7. In [5]: ts.plot()
  8. Out[5]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505e618d0>

../_images/series_plot_basic.pngIf the index consists of dates, it calls gcf().autofmt_xdate()to try to format the x-axis nicely as per above.

On DataFrame, plot() is a convenience to plot all of the columns with labels:

  1. In [6]: df = pd.DataFrame(np.random.randn(1000, 4),
  2. ...: index=ts.index, columns=list('ABCD'))
  3. ...:
  4.  
  5. In [7]: df = df.cumsum()
  6.  
  7. In [8]: plt.figure();
  8.  
  9. In [9]: df.plot();

../_images/frame_plot_basic.pngYou can plot one column versus another using the x and y keywords inplot():

  1. In [10]: df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
  2.  
  3. In [11]: df3['A'] = pd.Series(list(range(len(df))))
  4.  
  5. In [12]: df3.plot(x='A', y='B')
  6. Out[12]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505c26950>

../_images/df_plot_xy.png

Note

For more formatting and styling options, seeformatting below.

Other plots

Plotting methods allow for a handful of plot styles other than thedefault line plot. These methods can be provided as the kindkeyword argument to plot(), and include:

For example, a bar plot can be created the following way:

  1. In [13]: plt.figure();
  2.  
  3. In [14]: df.iloc[5].plot(kind='bar');

../_images/bar_plot_ex.pngYou can also create these other plots using the methods DataFrame.plot.<kind> instead of providing the kind keyword argument. This makes it easier to discover plot methods and the specific arguments they use:

  1. In [15]: df = pd.DataFrame()
  2.  
  3. In [16]: df.plot.<TAB> # noqa: E225, E999
  4. df.plot.area df.plot.barh df.plot.density df.plot.hist df.plot.line df.plot.scatter
  5. df.plot.bar df.plot.box df.plot.hexbin df.plot.kde df.plot.pie

In addition to these kind s, there are the DataFrame.hist(),and DataFrame.boxplot() methods, which use a separate interface.

Finally, there are several plotting functions in pandas.plottingthat take a Series or DataFrame as an argument. Theseinclude:

Plots may also be adorned with errorbarsor tables.

Bar plots

For labeled, non-time series data, you may wish to produce a bar plot:

  1. In [17]: plt.figure();
  2.  
  3. In [18]: df.iloc[5].plot.bar()
  4. Out[18]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505b6f890>
  5.  
  6. In [19]: plt.axhline(0, color='k');

../_images/bar_plot_ex.pngCalling a DataFrame’s plot.bar() method produces a multiplebar plot:

  1. In [20]: df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
  2.  
  3. In [21]: df2.plot.bar();

../_images/bar_plot_multi_ex.pngTo produce a stacked bar plot, pass stacked=True:

  1. In [22]: df2.plot.bar(stacked=True);

../_images/bar_plot_stacked_ex.pngTo get horizontal bar plots, use the barh method:

  1. In [23]: df2.plot.barh(stacked=True);

../_images/barh_plot_stacked_ex.png

Histograms

Histograms can be drawn by using the DataFrame.plot.hist() and Series.plot.hist() methods.

  1. In [24]: df4 = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
  2. ....: 'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
  3. ....:
  4.  
  5. In [25]: plt.figure();
  6.  
  7. In [26]: df4.plot.hist(alpha=0.5)
  8. Out[26]: <matplotlib.axes._subplots.AxesSubplot at 0x7f45058ea250>

../_images/hist_new.pngA histogram can be stacked using stacked=True. Bin size can be changedusing the bins keyword.

  1. In [27]: plt.figure();
  2.  
  3. In [28]: df4.plot.hist(stacked=True, bins=20)
  4. Out[28]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505902810>

../_images/hist_new_stacked.pngYou can pass other keywords supported by matplotlib hist. For example,horizontal and cumulative histograms can be drawn byorientation='horizontal' and cumulative=True.

  1. In [29]: plt.figure();
  2.  
  3. In [30]: df4['a'].plot.hist(orientation='horizontal', cumulative=True)
  4. Out[30]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505740610>

../_images/hist_new_kwargs.pngSee the hist method and thematplotlib hist documentation for more.

The existing interface DataFrame.hist to plot histogram still can be used.

  1. In [31]: plt.figure();
  2.  
  3. In [32]: df['A'].diff().hist()
  4. Out[32]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505656590>

../_images/hist_plot_ex.pngDataFrame.hist() plots the histograms of the columns on multiplesubplots:

  1. In [33]: plt.figure()
  2. Out[33]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [34]: df.diff().hist(color='k', alpha=0.5, bins=50)
  5. Out[34]:
  6. array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f45055e3f10>,
  7. <matplotlib.axes._subplots.AxesSubplot object at 0x7f45055a5790>],
  8. [<matplotlib.axes._subplots.AxesSubplot object at 0x7f450555aa90>,
  9. <matplotlib.axes._subplots.AxesSubplot object at 0x7f4505592d90>]],
  10. dtype=object)

../_images/frame_hist_ex.pngThe by keyword can be specified to plot grouped histograms:

  1. In [35]: data = pd.Series(np.random.randn(1000))
  2.  
  3. In [36]: data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4))
  4. Out[36]:
  5. array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f4505283790>,
  6. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450523cf10>],
  7. [<matplotlib.axes._subplots.AxesSubplot object at 0x7f4505200cd0>,
  8. <matplotlib.axes._subplots.AxesSubplot object at 0x7f45051b6fd0>]],
  9. dtype=object)

../_images/grouped_hist.png

Box plots

Boxplot can be drawn calling Series.plot.box() and DataFrame.plot.box(),or DataFrame.boxplot() to visualize the distribution of values within each column.

For instance, here is a boxplot representing five trials of 10 observations ofa uniform random variable on [0,1).

  1. In [37]: df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
  2.  
  3. In [38]: df.plot.box()
  4. Out[38]: <matplotlib.axes._subplots.AxesSubplot at 0x7f45050c6950>

../_images/box_plot_new.pngBoxplot can be colorized by passing color keyword. You can pass a dictwhose keys are boxes, whiskers, medians and caps.If some keys are missing in the dict, default colors are usedfor the corresponding artists. Also, boxplot has sym keyword to specify fliers style.

When you pass other type of arguments via color keyword, it will be directlypassed to matplotlib for all the boxes, whiskers, medians and capscolorization.

The colors are applied to every boxes to be drawn. If you wantmore complicated colorization, you can get each drawn artists by passingreturn_type.

  1. In [39]: color = {'boxes': 'DarkGreen', 'whiskers': 'DarkOrange',
  2. ....: 'medians': 'DarkBlue', 'caps': 'Gray'}
  3. ....:
  4.  
  5. In [40]: df.plot.box(color=color, sym='r+')
  6. Out[40]: <matplotlib.axes._subplots.AxesSubplot at 0x7f45050ab390>

../_images/box_new_colorize.pngAlso, you can pass other keywords supported by matplotlib boxplot.For example, horizontal and custom-positioned boxplot can be drawn byvert=False and positions keywords.

  1. In [41]: df.plot.box(vert=False, positions=[1, 4, 5, 6, 8])
  2. Out[41]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450502ae50>

../_images/box_new_kwargs.pngSee the boxplot method and thematplotlib boxplot documentation for more.

The existing interface DataFrame.boxplot to plot boxplot still can be used.

  1. In [42]: df = pd.DataFrame(np.random.rand(10, 5))
  2.  
  3. In [43]: plt.figure();
  4.  
  5. In [44]: bp = df.boxplot()

../_images/box_plot_ex.pngYou can create a stratified boxplot using the by keyword argument to creategroupings. For instance,

  1. In [45]: df = pd.DataFrame(np.random.rand(10, 2), columns=['Col1', 'Col2'])
  2.  
  3. In [46]: df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'])
  4.  
  5. In [47]: plt.figure();
  6.  
  7. In [48]: bp = df.boxplot(by='X')

../_images/box_plot_ex2.pngYou can also pass a subset of columns to plot, as well as group by multiplecolumns:

  1. In [49]: df = pd.DataFrame(np.random.rand(10, 3), columns=['Col1', 'Col2', 'Col3'])
  2.  
  3. In [50]: df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'])
  4.  
  5. In [51]: df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'])
  6.  
  7. In [52]: plt.figure();
  8.  
  9. In [53]: bp = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])

../_images/box_plot_ex3.png

Warning

The default changed from 'dict' to 'axes' in version 0.19.0.

In boxplot, the return type can be controlled by the return_type, keyword. The valid choices are {"axes", "dict", "both", None}.Faceting, created by DataFrame.boxplot with the bykeyword, will affect the output type as well:

return_type=FacetedOutput type
NoneNoaxes
NoneYes2-D ndarray of axes
'axes'Noaxes
'axes'YesSeries of axes
'dict'Nodict of artists
'dict'YesSeries of dicts of artists
'both'Nonamedtuple
'both'YesSeries of namedtuples

Groupby.boxplot always returns a Series of return_type.

  1. In [54]: np.random.seed(1234)
  2.  
  3. In [55]: df_box = pd.DataFrame(np.random.randn(50, 2))
  4.  
  5. In [56]: df_box['g'] = np.random.choice(['A', 'B'], size=50)
  6.  
  7. In [57]: df_box.loc[df_box['g'] == 'B', 1] += 3
  8.  
  9. In [58]: bp = df_box.boxplot(by='g')

../_images/boxplot_groupby.pngThe subplots above are split by the numeric columns first, then the value ofthe g column. Below the subplots are first split by the value of g,then by the numeric columns.

  1. In [59]: bp = df_box.groupby('g').boxplot()

../_images/groupby_boxplot_vis.png

Area plot

You can create area plots with Series.plot.area() and DataFrame.plot.area().Area plots are stacked by default. To produce stacked area plot, each column must be either all positive or all negative values.

When input data contains NaN, it will be automatically filled by 0. If you want to drop or fill by different values, use dataframe.dropna() or dataframe.fillna() before calling plot.

  1. In [60]: df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
  2.  
  3. In [61]: df.plot.area();

../_images/area_plot_stacked.pngTo produce an unstacked plot, pass stacked=False. Alpha value is set to 0.5 unless otherwise specified:

  1. In [62]: df.plot.area(stacked=False);

../_images/area_plot_unstacked.png

Scatter plot

Scatter plot can be drawn by using the DataFrame.plot.scatter() method.Scatter plot requires numeric columns for the x and y axes.These can be specified by the x and y keywords.

  1. In [63]: df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
  2.  
  3. In [64]: df.plot.scatter(x='a', y='b');

../_images/scatter_plot.pngTo plot multiple column groups in a single axes, repeat plot method specifying target ax.It is recommended to specify color and label keywords to distinguish each groups.

  1. In [65]: ax = df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1');
  2.  
  3. In [66]: df.plot.scatter(x='c', y='d', color='DarkGreen', label='Group 2', ax=ax);

../_images/scatter_plot_repeated.pngThe keyword c may be given as the name of a column to provide colors foreach point:

  1. In [67]: df.plot.scatter(x='a', y='b', c='c', s=50);

../_images/scatter_plot_colored.pngYou can pass other keywords supported by matplotlibscatter. The example below shows abubble chart using a column of the DataFrame as the bubble size.

  1. In [68]: df.plot.scatter(x='a', y='b', s=df['c'] * 200);

../_images/scatter_plot_bubble.pngSee the scatter method and thematplotlib scatter documentation for more.

Hexagonal bin plot

You can create hexagonal bin plots with DataFrame.plot.hexbin().Hexbin plots can be a useful alternative to scatter plots if your data aretoo dense to plot each point individually.

  1. In [69]: df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
  2.  
  3. In [70]: df['b'] = df['b'] + np.arange(1000)
  4.  
  5. In [71]: df.plot.hexbin(x='a', y='b', gridsize=25)
  6. Out[71]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4527dca2d0>

../_images/hexbin_plot.pngA useful keyword argument is gridsize; it controls the number of hexagonsin the x-direction, and defaults to 100. A larger gridsize means more, smallerbins.

By default, a histogram of the counts around each (x, y) point is computed.You can specify alternative aggregations by passing values to the C andreduce_C_function arguments. C specifies the value at each (x, y) pointand reduce_C_function is a function of one argument that reduces all thevalues in a bin to a single number (e.g. mean, max, sum, std). In thisexample the positions are given by columns a and b, while the value isgiven by column z. The bins are aggregated with NumPy’s max function.

  1. In [72]: df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
  2.  
  3. In [73]: df['b'] = df['b'] = df['b'] + np.arange(1000)
  4.  
  5. In [74]: df['z'] = np.random.uniform(0, 3, 1000)
  6.  
  7. In [75]: df.plot.hexbin(x='a', y='b', C='z', reduce_C_function=np.max, gridsize=25)
  8. Out[75]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450ab83ad0>

../_images/hexbin_plot_agg.pngSee the hexbin method and thematplotlib hexbin documentation for more.

Pie plot

You can create a pie plot with DataFrame.plot.pie() or Series.plot.pie().If your data includes any NaN, they will be automatically filled with 0.A ValueError will be raised if there are any negative values in your data.

  1. In [76]: series = pd.Series(3 * np.random.rand(4),
  2. ....: index=['a', 'b', 'c', 'd'], name='series')
  3. ....:
  4.  
  5. In [77]: series.plot.pie(figsize=(6, 6))
  6. Out[77]: <matplotlib.axes._subplots.AxesSubplot at 0x7f45102701d0>

../_images/series_pie_plot.pngFor pie plots it’s best to use square figures, i.e. a figure aspect ratio 1.You can create the figure with equal width and height, or force the aspect ratioto be equal after plotting by calling ax.set_aspect('equal') on the returnedaxes object.

Note that pie plot with DataFrame requires that you either specify atarget column by the y argument or subplots=True. When y isspecified, pie plot of selected column will be drawn. If subplots=True isspecified, pie plots for each column are drawn as subplots. A legend will bedrawn in each pie plots by default; specify legend=False to hide it.

  1. In [78]: df = pd.DataFrame(3 * np.random.rand(4, 2),
  2. ....: index=['a', 'b', 'c', 'd'], columns=['x', 'y'])
  3. ....:
  4.  
  5. In [79]: df.plot.pie(subplots=True, figsize=(8, 4))
  6. Out[79]:
  7. array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f452ed2bd10>,
  8. <matplotlib.axes._subplots.AxesSubplot object at 0x7f45203fa190>],
  9. dtype=object)

../_images/df_pie_plot.pngYou can use the labels and colors keywords to specify the labels and colors of each wedge.

Warning

Most pandas plots use the label and color arguments (note the lack of “s” on those).To be consistent with matplotlib.pyplot.pie() you must use labels and colors.

If you want to hide wedge labels, specify labels=None.If fontsize is specified, the value will be applied to wedge labels.Also, other keywords supported by matplotlib.pyplot.pie() can be used.

  1. In [80]: series.plot.pie(labels=['AA', 'BB', 'CC', 'DD'], colors=['r', 'g', 'b', 'c'],
  2. ....: autopct='%.2f', fontsize=20, figsize=(6, 6))
  3. ....:
  4. Out[80]: <matplotlib.axes._subplots.AxesSubplot at 0x7f45098c6910>

../_images/series_pie_plot_options.pngIf you pass values whose sum total is less than 1.0, matplotlib draws a semicircle.

  1. In [81]: series = pd.Series([0.1] * 4, index=['a', 'b', 'c', 'd'], name='series2')
  2.  
  3. In [82]: series.plot.pie(figsize=(6, 6))
  4. Out[82]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4509d83590>

../_images/series_pie_plot_semi.pngSee the matplotlib pie documentation for more.

Plotting with missing data

Pandas tries to be pragmatic about plotting DataFrames or Seriesthat contain missing data. Missing values are dropped, left out, or filleddepending on the plot type.

Plot TypeNaN Handling
LineLeave gaps at NaNs
Line (stacked)Fill 0’s
BarFill 0’s
ScatterDrop NaNs
HistogramDrop NaNs (column-wise)
BoxDrop NaNs (column-wise)
AreaFill 0’s
KDEDrop NaNs (column-wise)
HexbinDrop NaNs
PieFill 0’s

If any of these defaults are not what you want, or if you want to beexplicit about how missing values are handled, consider usingfillna() or dropna()before plotting.

Plotting Tools

These functions can be imported from pandas.plottingand take a Series or DataFrame as an argument.

Scatter matrix plot

You can create a scatter plot matrix using thescatter_matrix method in pandas.plotting:

  1. In [83]: from pandas.plotting import scatter_matrix
  2.  
  3. In [84]: df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])
  4.  
  5. In [85]: scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')
  6. Out[85]:
  7. array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f4509c45110>,
  8. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a111a90>,
  9. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a2743d0>,
  10. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a31c750>],
  11. [<matplotlib.axes._subplots.AxesSubplot object at 0x7f450a3bfa10>,
  12. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a5dbc10>,
  13. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a61ad90>,
  14. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a737c10>],
  15. [<matplotlib.axes._subplots.AxesSubplot object at 0x7f450a728b50>,
  16. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450a7a1c90>,
  17. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450ab46d50>,
  18. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450ab01590>],
  19. [<matplotlib.axes._subplots.AxesSubplot object at 0x7f450ae31f50>,
  20. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450b440990>,
  21. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450b645f50>,
  22. <matplotlib.axes._subplots.AxesSubplot object at 0x7f450b6c3350>]],
  23. dtype=object)

../_images/scatter_matrix_kde.png

Density plot

You can create density plots using the Series.plot.kde() and DataFrame.plot.kde() methods.

  1. In [86]: ser = pd.Series(np.random.randn(1000))
  2.  
  3. In [87]: ser.plot.kde()
  4. Out[87]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4526f94dd0>

../_images/kde_plot.png

Andrews curves

Andrews curves allow one to plot multivariate data as a large numberof curves that are created using the attributes of samples as coefficientsfor Fourier series, see the Wikipedia entryfor more information. By coloring these curves differently for each classit is possible to visualize data clustering. Curves belonging to samplesof the same class will usually be closer together and form larger structures.

Note: The “Iris” dataset is available here.

  1. In [88]: from pandas.plotting import andrews_curves
  2.  
  3. In [89]: data = pd.read_csv('data/iris.data')
  4.  
  5. In [90]: plt.figure()
  6. Out[90]: <Figure size 640x480 with 0 Axes>
  7.  
  8. In [91]: andrews_curves(data, 'Name')
  9. Out[91]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450babca10>

../_images/andrews_curves.png

Parallel coordinates

Parallel coordinates is a plotting technique for plotting multivariate data,see the Wikipedia entryfor an introduction.Parallel coordinates allows one to see clusters in data and to estimate other statistics visually.Using parallel coordinates points are represented as connected line segments.Each vertical line represents one attribute. One set of connected line segmentsrepresents one data point. Points that tend to cluster will appear closer together.

  1. In [92]: from pandas.plotting import parallel_coordinates
  2.  
  3. In [93]: data = pd.read_csv('data/iris.data')
  4.  
  5. In [94]: plt.figure()
  6. Out[94]: <Figure size 640x480 with 0 Axes>
  7.  
  8. In [95]: parallel_coordinates(data, 'Name')
  9. Out[95]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4510066850>

../_images/parallel_coordinates.png

Lag plot

Lag plots are used to check if a data set or time series is random. Randomdata should not exhibit any structure in the lag plot. Non-random structureimplies that the underlying data are not random. The lag argument maybe passed, and when lag=1 the plot is essentially data[:-1] vs.data[1:].

  1. In [96]: from pandas.plotting import lag_plot
  2.  
  3. In [97]: plt.figure()
  4. Out[97]: <Figure size 640x480 with 0 Axes>
  5.  
  6. In [98]: spacing = np.linspace(-99 * np.pi, 99 * np.pi, num=1000)
  7.  
  8. In [99]: data = pd.Series(0.1 * np.random.rand(1000) + 0.9 * np.sin(spacing))
  9.  
  10. In [100]: lag_plot(data)
  11. Out[100]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450ada7710>

../_images/lag_plot.png

Autocorrelation plot

Autocorrelation plots are often used for checking randomness in time series.This is done by computing autocorrelations for data values at varying time lags.If time series is random, such autocorrelations should be near zero for any andall time-lag separations. If time series is non-random then one or more of theautocorrelations will be significantly non-zero. The horizontal lines displayedin the plot correspond to 95% and 99% confidence bands. The dashed line is 99%confidence band. See theWikipedia entry for more aboutautocorrelation plots.

  1. In [101]: from pandas.plotting import autocorrelation_plot
  2.  
  3. In [102]: plt.figure()
  4. Out[102]: <Figure size 640x480 with 0 Axes>
  5.  
  6. In [103]: spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000)
  7.  
  8. In [104]: data = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing))
  9.  
  10. In [105]: autocorrelation_plot(data)
  11. Out[105]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450adaaf90>

../_images/autocorrelation_plot.png

Bootstrap plot

Bootstrap plots are used to visually assess the uncertainty of a statistic, suchas mean, median, midrange, etc. A random subset of a specified size is selectedfrom a data set, the statistic in question is computed for this subset and theprocess is repeated a specified number of times. Resulting plots and histogramsare what constitutes the bootstrap plot.

  1. In [106]: from pandas.plotting import bootstrap_plot
  2.  
  3. In [107]: data = pd.Series(np.random.rand(1000))
  4.  
  5. In [108]: bootstrap_plot(data, size=50, samples=500, color='grey')
  6. Out[108]: <Figure size 640x480 with 6 Axes>

../_images/bootstrap_plot.png

RadViz

RadViz is a way of visualizing multi-variate data. It is based on a simplespring tension minimization algorithm. Basically you set up a bunch of points ina plane. In our case they are equally spaced on a unit circle. Each pointrepresents a single attribute. You then pretend that each sample in the data setis attached to each of these points by a spring, the stiffness of which isproportional to the numerical value of that attribute (they are normalized tounit interval). The point in the plane, where our sample settles to (where theforces acting on our sample are at an equilibrium) is where a dot representingour sample will be drawn. Depending on which class that sample belongs it willbe colored differently.See the R package Radvizfor more information.

Note: The “Iris” dataset is available here.

  1. In [109]: from pandas.plotting import radviz
  2.  
  3. In [110]: data = pd.read_csv('data/iris.data')
  4.  
  5. In [111]: plt.figure()
  6. Out[111]: <Figure size 640x480 with 0 Axes>
  7.  
  8. In [112]: radviz(data, 'Name')
  9. Out[112]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450a99bd90>

../_images/radviz.png

Plot Formatting

Setting the plot style

From version 1.5 and up, matplotlib offers a range of pre-configured plotting styles. Setting thestyle can be used to easily give plots the general look that you want.Setting the style is as easy as calling matplotlib.style.use(my_plot_style) beforecreating your plot. For example you could write matplotlib.style.use('ggplot') for ggplot-styleplots.

You can see the various available style names at matplotlib.style.available and it’s veryeasy to try them out.

General plot style arguments

Most plotting methods have a set of keyword arguments that control thelayout and formatting of the returned plot:

  1. In [113]: plt.figure();
  2.  
  3. In [114]: ts.plot(style='k--', label='Series');

../_images/series_plot_basic2.pngFor each kind of plot (e.g. line, bar, scatter) any additional argumentskeywords are passed along to the corresponding matplotlib function(ax.plot(),ax.bar(),ax.scatter()). These can be usedto control additional styling, beyond what pandas provides.

Controlling the legend

You may set the legend argument to False to hide the legend, which isshown by default.

  1. In [115]: df = pd.DataFrame(np.random.randn(1000, 4),
  2. .....: index=ts.index, columns=list('ABCD'))
  3. .....:
  4.  
  5. In [116]: df = df.cumsum()
  6.  
  7. In [117]: df.plot(legend=False)
  8. Out[117]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450a24c2d0>

../_images/frame_plot_basic_noleg.png

Scales

You may pass logy to get a log-scale Y axis.

  1. In [118]: ts = pd.Series(np.random.randn(1000),
  2. .....: index=pd.date_range('1/1/2000', periods=1000))
  3. .....:
  4.  
  5. In [119]: ts = np.exp(ts.cumsum())
  6.  
  7. In [120]: ts.plot(logy=True)
  8. Out[120]: <matplotlib.axes._subplots.AxesSubplot at 0x7f45101effd0>

../_images/series_plot_logy.pngSee also the logx and loglog keyword arguments.

Plotting on a secondary y-axis

To plot data on a secondary y-axis, use the secondary_y keyword:

  1. In [121]: df.A.plot()
  2. Out[121]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450b2f6710>
  3.  
  4. In [122]: df.B.plot(secondary_y=True, style='g')
  5. Out[122]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450a6d7d50>

../_images/series_plot_secondary_y.pngTo plot some columns in a DataFrame, give the column names to the secondary_ykeyword:

  1. In [123]: plt.figure()
  2. Out[123]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [124]: ax = df.plot(secondary_y=['A', 'B'])
  5.  
  6. In [125]: ax.set_ylabel('CD scale')
  7. Out[125]: Text(0, 0.5, 'CD scale')
  8.  
  9. In [126]: ax.right_ax.set_ylabel('AB scale')
  10. Out[126]: Text(0, 0.5, 'AB scale')

../_images/frame_plot_secondary_y.pngNote that the columns plotted on the secondary y-axis is automatically markedwith “(right)” in the legend. To turn off the automatic marking, use themark_right=False keyword:

  1. In [127]: plt.figure()
  2. Out[127]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [128]: df.plot(secondary_y=['A', 'B'], mark_right=False)
  5. Out[128]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450bfdde50>

../_images/frame_plot_secondary_y_no_right.png

Suppressing tick resolution adjustment

pandas includes automatic tick resolution adjustment for regular frequencytime-series data. For limited cases where pandas cannot infer the frequencyinformation (e.g., in an externally created twinx), you can choose tosuppress this behavior for alignment purposes.

Here is the default behavior, notice how the x-axis tick labeling is performed:

  1. In [129]: plt.figure()
  2. Out[129]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [130]: df.A.plot()
  5. Out[130]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4505cd2110>

../_images/ser_plot_suppress.pngUsing the x_compat parameter, you can suppress this behavior:

  1. In [131]: plt.figure()
  2. Out[131]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [132]: df.A.plot(x_compat=True)
  5. Out[132]: <matplotlib.axes._subplots.AxesSubplot at 0x7f452b3b9090>

../_images/ser_plot_suppress_parm.pngIf you have more than one plot that needs to be suppressed, the use methodin pandas.plotting.plotparams can be used in a _with statement:

  1. In [133]: plt.figure()
  2. Out[133]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [134]: with pd.plotting.plot_params.use('x_compat', True):
  5. .....: df.A.plot(color='r')
  6. .....: df.B.plot(color='g')
  7. .....: df.C.plot(color='b')
  8. .....:

../_images/ser_plot_suppress_context.png

Automatic date tick adjustment

New in version 0.20.0.

TimedeltaIndex now uses the native matplotlibtick locator methods, it is useful to call the automaticdate tick adjustment from matplotlib for figures whose ticklabels overlap.

See the autofmt_xdate method and thematplotlib documentation for more.

Subplots

Each Series in a DataFrame can be plotted on a different axiswith the subplots keyword:

  1. In [135]: df.plot(subplots=True, figsize=(6, 6));

../_images/frame_plot_subplots.png

Using layout and targeting multiple axes

The layout of subplots can be specified by the layout keyword. It can accept(rows, columns). The layout keyword can be used inhist and boxplot also. If the input is invalid, a ValueError will be raised.

The number of axes which can be contained by rows x columns specified by layout must belarger than the number of required subplots. If layout can contain more axes than required,blank axes are not drawn. Similar to a NumPy array’s reshape method, youcan use -1 for one dimension to automatically calculate the number of rowsor columns needed, given the other.

  1. In [136]: df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False);

../_images/frame_plot_subplots_layout.pngThe above example is identical to using:

  1. In [137]: df.plot(subplots=True, layout=(2, -1), figsize=(6, 6), sharex=False);

The required number of columns (3) is inferred from the number of series to plotand the given number of rows (2).

You can pass multiple axes created beforehand as list-like via ax keyword.This allows more complicated layouts.The passed axes must be the same number as the subplots being drawn.

When multiple axes are passed via the ax keyword, layout, sharex and sharey keywordsdon’t affect to the output. You should explicitly pass sharex=False and sharey=False,otherwise you will see a warning.

  1. In [138]: fig, axes = plt.subplots(4, 4, figsize=(6, 6))
  2.  
  3. In [139]: plt.subplots_adjust(wspace=0.5, hspace=0.5)
  4.  
  5. In [140]: target1 = [axes[0][0], axes[1][1], axes[2][2], axes[3][3]]
  6.  
  7. In [141]: target2 = [axes[3][0], axes[2][1], axes[1][2], axes[0][3]]
  8.  
  9. In [142]: df.plot(subplots=True, ax=target1, legend=False, sharex=False, sharey=False);
  10.  
  11. In [143]: (-df).plot(subplots=True, ax=target2, legend=False,
  12. .....: sharex=False, sharey=False);
  13. .....:

../_images/frame_plot_subplots_multi_ax.pngAnother option is passing an ax argument to Series.plot() to plot on a particular axis:

  1. In [144]: fig, axes = plt.subplots(nrows=2, ncols=2)
  2.  
  3. In [145]: df['A'].plot(ax=axes[0, 0]);
  4.  
  5. In [146]: axes[0, 0].set_title('A');
  6.  
  7. In [147]: df['B'].plot(ax=axes[0, 1]);
  8.  
  9. In [148]: axes[0, 1].set_title('B');
  10.  
  11. In [149]: df['C'].plot(ax=axes[1, 0]);
  12.  
  13. In [150]: axes[1, 0].set_title('C');
  14.  
  15. In [151]: df['D'].plot(ax=axes[1, 1]);
  16.  
  17. In [152]: axes[1, 1].set_title('D');

../_images/series_plot_multi.png

Plotting with error bars

Plotting with error bars is supported in DataFrame.plot() and Series.plot().

Horizontal and vertical error bars can be supplied to the xerr and yerr keyword arguments to plot(). The error values can be specified using a variety of formats:

  • As a DataFrame or dict of errors with column names matching the columns attribute of the plotting DataFrame or matching the name attribute of the Series.
  • As a str indicating which of the columns of plotting DataFrame contain the error values.
  • As raw values (list, tuple, or np.ndarray). Must be the same length as the plotting DataFrame/Series.

Asymmetrical error bars are also supported, however raw error values must be provided in this case. For a M length Series, a Mx2 array should be provided indicating lower and upper (or left and right) errors. For a MxN DataFrame, asymmetrical errors should be in a Mx2xN array.

Here is an example of one way to easily plot group means with standard deviations from the raw data.

  1. # Generate the data
  2. In [153]: ix3 = pd.MultiIndex.from_arrays([
  3. .....: ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'],
  4. .....: ['foo', 'foo', 'bar', 'bar', 'foo', 'foo', 'bar', 'bar']],
  5. .....: names=['letter', 'word'])
  6. .....:
  7.  
  8. In [154]: df3 = pd.DataFrame({'data1': [3, 2, 4, 3, 2, 4, 3, 2],
  9. .....: 'data2': [6, 5, 7, 5, 4, 5, 6, 5]}, index=ix3)
  10. .....:
  11.  
  12. # Group by index labels and take the means and standard deviations
  13. # for each group
  14. In [155]: gp3 = df3.groupby(level=('letter', 'word'))
  15.  
  16. In [156]: means = gp3.mean()
  17.  
  18. In [157]: errors = gp3.std()
  19.  
  20. In [158]: means
  21. Out[158]:
  22. data1 data2
  23. letter word
  24. a bar 3.5 6.0
  25. foo 2.5 5.5
  26. b bar 2.5 5.5
  27. foo 3.0 4.5
  28.  
  29. In [159]: errors
  30. Out[159]:
  31. data1 data2
  32. letter word
  33. a bar 0.707107 1.414214
  34. foo 0.707107 0.707107
  35. b bar 0.707107 0.707107
  36. foo 1.414214 0.707107
  37.  
  38. # Plot
  39. In [160]: fig, ax = plt.subplots()
  40.  
  41. In [161]: means.plot.bar(yerr=errors, ax=ax, capsize=4)
  42. Out[161]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4526df3d50>

../_images/errorbar_example.png

Plotting tables

Plotting with matplotlib table is now supported in DataFrame.plot() and Series.plot() with a table keyword. The table keyword can accept bool, DataFrame or Series. The simple way to draw a table is to specify table=True. Data will be transposed to meet matplotlib’s default layout.

  1. In [162]: fig, ax = plt.subplots(1, 1)
  2.  
  3. In [163]: df = pd.DataFrame(np.random.rand(5, 3), columns=['a', 'b', 'c'])
  4.  
  5. In [164]: ax.get_xaxis().set_visible(False) # Hide Ticks
  6.  
  7. In [165]: df.plot(table=True, ax=ax)
  8. Out[165]: <matplotlib.axes._subplots.AxesSubplot at 0x7f451011eb50>

../_images/line_plot_table_true.pngAlso, you can pass a different DataFrame or Series to thetable keyword. The data will be drawn as displayed in print method(not transposed automatically). If required, it should be transposed manuallyas seen in the example below.

  1. In [166]: fig, ax = plt.subplots(1, 1)
  2.  
  3. In [167]: ax.get_xaxis().set_visible(False) # Hide Ticks
  4.  
  5. In [168]: df.plot(table=np.round(df.T, 2), ax=ax)
  6. Out[168]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450be084d0>

../_images/line_plot_table_data.pngThere also exists a helper function pandas.plotting.table, which creates atable from DataFrame or Series, and adds it to anmatplotlib.Axes instance. This function can accept keywords which thematplotlib table has.

  1. In [169]: from pandas.plotting import table
  2.  
  3. In [170]: fig, ax = plt.subplots(1, 1)
  4.  
  5. In [171]: table(ax, np.round(df.describe(), 2),
  6. .....: loc='upper right', colWidths=[0.2, 0.2, 0.2])
  7. .....:
  8. Out[171]: <matplotlib.table.Table at 0x7f450a2eea90>
  9.  
  10. In [172]: df.plot(ax=ax, ylim=(0, 2), legend=None)
  11. Out[172]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450b936e10>

../_images/line_plot_table_describe.pngNote: You can get table instances on the axes using axes.tables property for further decorations. See the matplotlib table documentation for more.

Colormaps

A potential issue when plotting a large number of columns is that it can bedifficult to distinguish some series due to repetition in the default colors. Toremedy this, DataFrame plotting supports the use of the colormap argument,which accepts either a Matplotlib colormapor a string that is a name of a colormap registered with Matplotlib. Avisualization of the default matplotlib colormaps is available here.

As matplotlib does not directly support colormaps for line-based plots, thecolors are selected based on an even spacing determined by the number of columnsin the DataFrame. There is no consideration made for background color, so somecolormaps will produce lines that are not easily visible.

To use the cubehelix colormap, we can pass colormap='cubehelix'.

  1. In [173]: df = pd.DataFrame(np.random.randn(1000, 10), index=ts.index)
  2.  
  3. In [174]: df = df.cumsum()
  4.  
  5. In [175]: plt.figure()
  6. Out[175]: <Figure size 640x480 with 0 Axes>
  7.  
  8. In [176]: df.plot(colormap='cubehelix')
  9. Out[176]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450b1c69d0>

../_images/cubehelix.pngAlternatively, we can pass the colormap itself:

  1. In [177]: from matplotlib import cm
  2.  
  3. In [178]: plt.figure()
  4. Out[178]: <Figure size 640x480 with 0 Axes>
  5.  
  6. In [179]: df.plot(colormap=cm.cubehelix)
  7. Out[179]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4509948d50>

../_images/cubehelix_cm.pngColormaps can also be used other plot types, like bar charts:

  1. In [180]: dd = pd.DataFrame(np.random.randn(10, 10)).applymap(abs)
  2.  
  3. In [181]: dd = dd.cumsum()
  4.  
  5. In [182]: plt.figure()
  6. Out[182]: <Figure size 640x480 with 0 Axes>
  7.  
  8. In [183]: dd.plot.bar(colormap='Greens')
  9. Out[183]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450b722b10>

../_images/greens.pngParallel coordinates charts:

  1. In [184]: plt.figure()
  2. Out[184]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [185]: parallel_coordinates(data, 'Name', colormap='gist_rainbow')
  5. Out[185]: <matplotlib.axes._subplots.AxesSubplot at 0x7f4509d5f250>

../_images/parallel_gist_rainbow.pngAndrews curves charts:

  1. In [186]: plt.figure()
  2. Out[186]: <Figure size 640x480 with 0 Axes>
  3.  
  4. In [187]: andrews_curves(data, 'Name', colormap='winter')
  5. Out[187]: <matplotlib.axes._subplots.AxesSubplot at 0x7f450a3f3990>

../_images/andrews_curve_winter.png

Plotting directly with matplotlib

In some situations it may still be preferable or necessary to prepare plotsdirectly with matplotlib, for instance when a certain type of plot orcustomization is not (yet) supported by pandas. Series and DataFrameobjects behave like arrays and can therefore be passed directly tomatplotlib functions without explicit casts.

pandas also automatically registers formatters and locators that recognize dateindices, thereby extending date and time support to practically all plot typesavailable in matplotlib. Although this formatting does not provide the samelevel of refinement you would get when plotting via pandas, it can be fasterwhen plotting a large number of points.

  1. In [188]: price = pd.Series(np.random.randn(150).cumsum(),
  2. .....: index=pd.date_range('2000-1-1', periods=150, freq='B'))
  3. .....:
  4.  
  5. In [189]: ma = price.rolling(20).mean()
  6.  
  7. In [190]: mstd = price.rolling(20).std()
  8.  
  9. In [191]: plt.figure()
  10. Out[191]: <Figure size 640x480 with 0 Axes>
  11.  
  12. In [192]: plt.plot(price.index, price, 'k')
  13. Out[192]: [<matplotlib.lines.Line2D at 0x7f450a7e5d90>]
  14.  
  15. In [193]: plt.plot(ma.index, ma, 'b')
  16. Out[193]: [<matplotlib.lines.Line2D at 0x7f4526e3a650>]
  17.  
  18. In [194]: plt.fill_between(mstd.index, ma - 2 * mstd, ma + 2 * mstd,
  19. .....: color='b', alpha=0.2)
  20. .....:
  21. Out[194]: <matplotlib.collections.PolyCollection at 0x7f450ad7f690>

../_images/bollinger.png

Trellis plotting interface

Warning

The rplot trellis plotting interface has been removed. Please useexternal packages like seaborn forsimilar but more refined functionality and refer to our 0.18.1 documentationherefor how to convert to using it.