Why is it that the first plot processed by Matplotlib or pandas df.plot runs extremely slowly compared to all subsequent plots. This even occurs when the subsequent plots use completely different variables. Is there some solution to increase the speed of the first plot?
An example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import time
import random
# 1st DataFrame
data1 = {
'X': range(10),
'Y': [x**2 for x in random.sample(range(1, 100), 10)],
'Z': [x*2 for x in random.sample(range(1, 100), 10)]
}
df1 = pd.DataFrame(data1)
# 2nd DataFrame
data2 = {
'X': range(10),
'Y': [x**2 for x in random.sample(range(1, 100), 10)],
'Z': [x*2 for x in random.sample(range(1, 100), 10)]
}
df2 = pd.DataFrame(data2)
# 3rd DataFrame
data3 = {
'X': range(10),
'Y': [x**2 for x in random.sample(range(1, 100), 10)],
'Z': [x*2 for x in random.sample(range(1, 100), 10)]
}
df3 = pd.DataFrame(data3)
# Plot and save 1st df using pandas plot
start = time.time()
ax1 = df1.plot(x='X', y=['Y', 'Z'], figsize=(8, 6), marker='o', title='DataFrame 1 Plot')
ax1.set_xlabel('X')
ax1.set_ylabel('Values')
ax1.grid(True)
plt.savefig('dataframe1_plot.png')
plt.close()
end = time.time()
print(f"Time for plot 1: {end - start:.2f} seconds")
# Plot and save 2nd df using pandas plot
start = time.time()
ax2 = df2.plot(x='X', y=['Y', 'Z'], figsize=(8, 6), marker='o', title='DataFrame 2 Plot')
ax2.set_xlabel('X')
ax2.set_ylabel('Values')
ax2.grid(True)
plt.savefig('dataframe2_plot.png')
plt.close()
end = time.time()
print(f"Time for plot 2: {end - start:.2f} seconds")
# Plot and save 3rd df using pandas plot
start = time.time()
ax3 = df3.plot(x='X', y=['Y', 'Z'], figsize=(8, 6), marker='o', title='DataFrame 3 Plot')
ax3.set_xlabel('X')
ax3.set_ylabel('Values')
ax3.grid(True)
plt.savefig('dataframe3_plot.png')
plt.close()
end = time.time()
print(f"Time for plot 3: {end - start:.2f} seconds")
When I look at the time.time results, I always get something that looks like this:
Time for plot 1: 134.06 seconds
Time for plot 2: 0.29 seconds
Time for plot 3: 0.23 seconds