Hong
Hong

Reputation: 263

python plot several figures

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]})

Upvotes: 0

Views: 5112

Answers (2)

zabop
zabop

Reputation: 7852

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.

Upvotes: 1

B.Sarah
B.Sarah

Reputation: 139

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() 

Upvotes: 2

Related Questions