使用matplotlib自帶的幾種美化樣式,,就可以很輕松的對生成的圖形進(jìn)行美化。
可以使用matplotlib.pyplot.style.available獲取所有的美化樣式
- #!/usr/bin/python
- #coding: utf-8
-
- import numpy as np
- import matplotlib.pyplot as plt
-
- # 獲取所有的自帶樣式
- print plt.style.available
-
- # 使用自帶的樣式進(jìn)行美化
- plt.style.use("ggplot")
-
- fig, axes = plt.subplots(ncols = 2, nrows = 2)
-
- # 四個子圖的坐標(biāo)軸賦予四個對象
- ax1, ax2, ax3, ax4 = axes.ravel()
-
- x, y = np.random.normal(size = (2, 100))
- ax1.plot(x, y, "o")
-
- x = np.arange(1, 10)
- y = np.arange(1, 10)
-
- # plt.rcParams['axes.prop_cycle']獲取顏色的字典
- # 會在這個范圍內(nèi)依次循環(huán)
- ncolors = len(plt.rcParams['axes.prop_cycle'])
- # print ncolors
- # print plt.rcParams['axes.prop_cycle']
-
- shift = np.linspace(1, 20, ncolors)
- for s in shift:
- # print s
- ax2.plot(x, y + s, "-")
-
- x = np.arange(5)
- y1, y2, y3 = np.random.randint(1, 25, size = (3, 5))
- width = 0.25
-
- # 柱狀圖中要顯式的指定顏色
- ax3.bar(x, y1, width, color = "r")
- ax3.bar(x + width, y2, width, color = "g")
- ax3.bar(x + 2 * width, y3, width, color = "y")
-
- for i, color in enumerate(plt.rcParams['axes.prop_cycle']):
- xy = np.random.normal(size= 2)
- for c in color.values():
- ax4.add_patch(plt.Circle(xy, radius = 0.3, color= c))
-
- ax4.axis("equal")
-
- plt.show()
使用ggplot進(jìn)行美化后的結(jié)果
|