使用Pandas进行数据可视化

箱型图

1
2
3
4
5
6
7
8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(8, 4), columns=['A','B','C','D'])

df.plot.box()
plt.show()

散点图

1
2
3
4
5
6
7
8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(8, 2), columns=['A','B'])

df.plot.scatter(x='A', y='B')
plt.show()

折线图

1
2
3
4
5
6
7
8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(8, 4), columns=['A','B','C','D'])

df['A'].plot()
plt.show()

堆叠柱状图

1
2
3
4
5
6
7
8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(8, 4), columns=['A','B','C','D'])

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

堆叠面积图

1
2
3
4
5
6
7
8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(8, 4), columns=['A','B','C','D'])

df.plot.area(stacked=True, alpha = 0.9)
plt.show()

堆叠/不堆叠的直方图

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'a': np.random.randn(1000) + 1,
'b': np.random.randn(1000),
'c': np.random.randn(1000) - 1},
columns=['a', 'b', 'c'])

df.plot.hist(stacked=True, bins=20)
df.plot.hist(alpha=0.5)
plt.show()

更多示例

0%