0

I have 50 csv files. I use "for loop" to get the dataframe. Now I want plot these 50 figures seperately. 6 subplots in 1 plot. How can I get this? Thanks a lot.

path = 'E:/XXXX/'
files = os.listdir(path)
files_csv = list(filter(lambda x: x[-4:]=='.csv' , files))
for file1 in  files_csv:
    tmp1=pd.read_csv(path + file1)

my data is like below:

df = pd.DataFrame({'date': [20121231,20130102, 20130105, 20130106, 20130107, 20130108],'price': [25, 163, 235, 36, 40, 82]})
Hong
  • 263
  • 2
  • 8
  • 1
    Please describe your data and explain why you expect 6 subplots. – Dirk Horsten Aug 28 '18 at 08:22
  • I post my data. Since I need plot 50 figures, if i choose 2 subplots in one plot. Then I will get 25 plots. – Hong Aug 28 '18 at 08:29
  • https://stackoverflow.com/questions/31726643/how-do-i-get-multiple-subplots-in-matplotlib/31728991 – Abhi Aug 28 '18 at 08:35
  • You can have 1 figure for each file, multiple data from that file in multiple subplots on each of those figures, or you could have all your files on the same plot on the same figure... What you need is unclear and you don't seem to differentiate a figure from an axis. – Mathieu Aug 28 '18 at 08:47

2 Answers2

2

You can create a figure for each frame and use matplotlib.pyplot.subplot function to plot your 6 different plots. Help yourself with the example bellow. Hope this helps.

from math import pi
import numpy as np 
import matplotlib.pyplot as plt

x1 = np.linspace(-2*pi, 2*pi, 50)
y1 = np.cos(x1)
x2 = np.linspace(-pi, pi, 50)
y2 = np.cos(x2)


plt.figure()
plt.grid(True)
plt.title('your title ' )
plt.subplot(121)
plt.plot(x1, y1, 'r', label= 'y1 = cos(x1)')
plt.legend(loc=1)
plt.subplot(122)
plt.plot(x2, y2, 'b', label = 'y2 = cos(x2)')
plt.legend(loc=1)

plt.show() 
B.Sarah
  • 119
  • 1
  • 3
  • 11
1
import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-1, 1, 50)

howmanyrowsyouwant = 1 # how many times 6 subplots you want

for x in range(howmanyrowsyouwant):
    _, ax = plt.subplots(nrows=1, ncols=6, figsize=(24,4))
    ax[0].set_title('title of first')
    ax[0].plot(x1) # plot for first subplot
    ax[1].set_title('title of second')
    ax[1].plot(x1) # plot for second subplot
    ax[2].set_title('title of third')
    ax[2].plot(x1) # plot for third subplot
    ax[3].set_title('title of fourth')
    ax[3].plot(x1) # plot for fourth subplot
    ax[4].set_title('title of fifth')
    ax[4].plot(x1) # plot for fifth subplot
    ax[5].set_title('title of sixth')
    ax[5].plot(x1) # plot for sixth subplot

This produces six subplots in a row, as many times as you specify.

zabop
  • 6,750
  • 3
  • 39
  • 84