1

I am building a GUI in PySide where I have to keep redrawing pandas.DataFrame objects.

I found out in this simple snippet of code that plotting the pandas DataFrame object df takes much longer to plot than the numpy.array object, despite the fact that the plots are nearly identical. This is too slow for my GUI. Why is this so much slower?

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data = np.cumsum(np.random.randn(100, 10), axis=0)
df = pd.DataFrame(data)

df.plot()  # Compare the speed of this line... (slow)
plt.plot(data)  # to this line. (fast)

I like the way that the pandas.DataFrame plots look, especially because in my real example my x-axis is datetime data from pandas. I do not know how to format a matplotlib.pyplot x-axis to look good with datetime data.

How do I speed up pandas.DataFrame plotting?

Paul Terwilliger
  • 1,596
  • 1
  • 20
  • 45
  • I think it would be an interesting observation if the pandas plot really took more time to plot than the matplotlib plot. However, I cannot reproduce this issue. When running the above, the plots appear instantaneously in both cases (which would also be line with my expectations, since pandas' plot function is only a wrapper for a matplotlib plot). Hence it would necessary to expand the question with measurable timings and their results (something like "df.plot() takes x milliseconds, plt.plot() takes y milliseconds" including the code used to time it). – ImportanceOfBeingErnest Sep 07 '17 at 19:25
  • 1
    If you cannot resolve the issue of the pandas plotting wrapper taking much more time (e.g. by reinstalling or updating pandas), you may indeed use matplotlib to plot your data. Plotting pandas dates series with matplotlib always goes along the scheme of (1) converting the dates to `datetime` objects (2) plotting (3) using a `matplotlib.dates`locator and formatter. There are numerous questions about that on SO, [here](https://stackoverflow.com/a/41199513/4124317) and [here](https://stackoverflow.com/a/44214830/4124317) are two that I have provided answers to, but you'll find many more. – ImportanceOfBeingErnest Sep 07 '17 at 19:40

0 Answers0