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()
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()
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With