简体   繁体   中英

Python: Using Pandas to plot more than one series in each subplot

Using python, I have created a pandas dataframe that contains four columns (date, df1, df2, df3). The 'date' series is the index for the dataframe.

I am trying to create two subplots (read: two stacked graphs), that share the 'date' series as the x-axis.

I would like to plot series1 and series 2 on the first subplot, using colors: black and blue.

In the second subplot I would like to plot series3, using the color: red.

Does anybody know how I can go about doing this using pandas or matplotlib?

Thanks in advance!

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

file1 = "LNQ2014LNV2014"
file2 = "LNV2014LNZ2014"
file3 = "LNQ2014LNV2014LNV2014LNZ2014"

df1 = pd.read_csv("%s.csv" % (file1), index_col = 0)
df1 = df1.drop(df1.columns[[0,1,2,4,5]], axis = 1)
df1.columns = ["%s" % file1]

df2 = pd.read_csv("%s.csv" % (file2), index_col = 0)
df2 = df2.drop(df2.columns[[0,1,2,4,5]], axis = 1)
df2.columns = ["%s" % file2]

df3 = pd.read_csv("%s.csv" % (file3), index_col = 0)
df3 = df3.drop(df3.columns[[0,1,2,4,5]], axis = 1)
df3.columns = ["%s" % file3]

dfresult = pd.concat([df1, df2, df3], axis = 1, join = "inner")

plt.subplot(211)
plt.plot(df1, color = "black")
plt.plot(df2, color = "blue")
plt.subplot(212)
plt.plot(df3, color = "red")

plt.show()

You'll want to use the subplot function. This creates 2 vertically stacked plots like you mentioned:

from matplotlib import pylab as pl

pl.subplot(211)
pl.plot(date, series1, color='black')
pl.plot(date, series2, color='blue')
pl.subplot(212)
pl.plot(date, series3, color='red')
pl.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM