python绘制几个数字

时间:2018-08-28 07:53:13

标签: python plot subplot

我有50个csv文件。我使用“ for循环”来获取数据帧。现在,我要分别绘制这50个数字。 1个地块中的6个子图。我怎么能得到这个?非常感谢。

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)

我的数据如下:

df = pd.DataFrame({'date': [20121231,20130102, 20130105, 20130106, 20130107, 20130108],'price': [25, 163, 235, 36, 40, 82]})

2 个答案:

答案 0 :(得分: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

这将连续产生六个子图,次数与您指定的次数相同。

答案 1 :(得分:0)

您可以为每个框架创建一个图形,并使用matplotlib.pyplot.subplot函数绘制6个不同的图形。下面的例子可以帮助您自己。希望这会有所帮助。

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