Reputation: 37
I am trying to plot multiple graphs into 1 using a for loop and I encountered this problem. I tried it for the other loops and it works just fine but I don't know what happened with this one.
The files used are the exchange rates of EUR to USD for the past 2 years and I am trying to plot the date and the price on the graph. If I don't use figsize the graph is too small but it works.
import pandas as pd
import matplotlib.pyplot as plt
file = ['somefile.csv', 'otherfile.csv', 'anotherfile.csv']
for files in file:
files1 = pd.read_csv ('%s' %files)
files1.plot (kind='line', x='Date', y='Price', ax=ax, figsize=(15,10))
plt.legend()
plt.show()
Upvotes: 2
Views: 18612
Reputation: 143
One way around is using
plt.gcf().set_size_inches(15, 8)
So your code should be
import pandas as pd
import matplotlib.pyplot as plt
file = ['somefile.csv', 'otherfile.csv', 'anotherfile.csv']
for files in file:
files1 = pd.read_csv ('%s' %files)
files1.plot (kind='line', x='Date', y='Price', ax=ax)
plt.gcf().set_size_inches(15, 8))
plt.legend()
plt.show()
Upvotes: 10
Reputation: 11
Since you want to specify axis as ax(ax=ax), best answer would be @Sheldore's. But there's actually an easier, also the most basic, way to this:
plt.figure(figsize=(15,10))
Put this before the for loop.
Upvotes: 1
Reputation: 39052
Use the following: Create first the axis object specifying the figure size and then use that object while plotting
fig, ax = plt.subplots(figsize=(15,10))
for files in file:
files1 = pd.read_csv ('%s' %files)
files1.plot (kind='line', x='Date', y='Price', ax=ax)
Upvotes: 1