Python Pandas 图形绘制(一):折线图,柱状图

Pandas 图形绘制(一):折线图,柱状图


一、折线图 Line Chart

1.1 默认绘制折线图 df.plot

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

df = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2020', periods=1000))
df = df.cumsum()
df.plot()
plt.show()

Figure_1.png

1.2 绘制多条折线

df = pd.DataFrame(np.random.randn(1000, 3), index=pd.date_range('1/1/2020', periods=1000), columns=['A','B','C'])
df = df.cumsum()
df.plot()
plt.show()

2.png

二、柱状图 Bar Chart

2.1 垂直柱状图 df.plot.bar

df_2 = pd.DataFrame(np.random.rand(10, 3), columns=['a', 'b', 'c'])
df_2.plot.bar()
plt.show()

3.png

2.2 叠加柱状图 stacked=True

df_2.plot.bar(stacked=True)
plt.show()

4.png

2.3 水平叠加柱状图 df.plot.barh

df_2.plot.barh(stacked=True)
plt.show()

5.png

Tags: