# _*_ coding: utf-8 _*_ """ python_visual.py by xianhu """ import numpy as np import matplotlib import matplotlib.mlab as mlab import matplotlib.pyplot as plt import matplotlib.font_manager as fm from mpl_toolkits.mplot3d import Axes3D # è§£å³ä¸æä¹±ç é®é¢ myfont = fm.FontProperties(fname="/Library/Fonts/Songti.ttc", size=14) matplotlib.rcParams["axes.unicode_minus"] = False def simple_plot(): """ simple plot """ # çææµè¯æ°æ® x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y_cos, y_sin = np.cos(x), np.sin(x) # çæç»å¸ï¼å¹¶è®¾å®æ é¢ plt.figure(figsize=(8, 6), dpi=80) plt.title("ç®åæ²çº¿å¾", fontproperties=myfont) plt.grid(True) # 设置Xè½´ plt.xlabel("Xè½´", fontproperties=myfont) plt.xlim(-4.0, 4.0) plt.xticks(np.linspace(-4, 4, 9, endpoint=True)) # 设置Yè½´ plt.ylabel("Yè½´", fontproperties=myfont) plt.ylim(-1.0, 1.0) plt.yticks(np.linspace(-1, 1, 9, endpoint=True)) # ç»ä¸¤æ¡æ²çº¿ plt.plot(x, y_cos, "b--", linewidth=2.0, label="cos示ä¾") plt.plot(x, y_sin, "g-", linewidth=2.0, label="sin示ä¾") # 设置å¾ä¾ä½ç½®,locå¯ä»¥ä¸º[upper, lower, left, right, center] plt.legend(loc="upper left", prop=myfont, shadow=True) # å¾å½¢æ¾ç¤º plt.show() return # simple_plot() def simple_advanced_plot(): """ simple advanced plot """ # çææµè¯æ°æ® x = np.linspace(-np.pi, np.pi, 256, endpoint=True) y_cos, y_sin = np.cos(x), np.sin(x) # çæç»å¸, å¹¶è®¾å®æ é¢ plt.figure(figsize=(8, 6), dpi=80) plt.title("夿æ²çº¿å¾", fontproperties=myfont) plt.grid(True) # ç»å¾çå¦å¤ä¸ç§æ¹å¼ ax_1 = plt.subplot(111) ax_1.plot(x, y_cos, color="blue", linewidth=2.0, linestyle="--", label="å·¦cos") ax_1.legend(loc="upper left", prop=myfont, shadow=True) # 设置Yè½´(左边) ax_1.set_ylabel("å·¦cosçyè½´", fontproperties=myfont) ax_1.set_ylim(-1.0, 1.0) ax_1.set_yticks(np.linspace(-1, 1, 9, endpoint=True)) # ç»å¾çå¦å¤ä¸ç§æ¹å¼ ax_2 = ax_1.twinx() ax_2.plot(x, y_sin, color="green", linewidth=2.0, linestyle="-", label="å³sin") ax_2.legend(loc="upper right", prop=myfont, shadow=True) # 设置Yè½´(å³è¾¹) ax_2.set_ylabel("å³sinçyè½´", fontproperties=myfont) ax_2.set_ylim(-2.0, 2.0) ax_2.set_yticks(np.linspace(-2, 2, 9, endpoint=True)) # 设置Xè½´(å ±å) ax_1.set_xlabel("xè½´", fontproperties=myfont) ax_1.set_xlim(-4.0, 4.0) ax_1.set_xticks(np.linspace(-4, 4, 9, endpoint=True)) # å¾å½¢æ¾ç¤º plt.show() return # simple_advanced_plot() def subplot_plot(): """ subplot plot """ # åå¾çstyleå表 style_list = ["g+-", "r*-", "b.-", "yo-"] # 便¬¡ç»å¾ for num in range(4): # çææµè¯æ°æ® x = np.linspace(0.0, 2+num, num=10*(num+1)) y = np.sin((5-num) * np.pi * x) # åå¾ççææ¹å¼ plt.subplot(2, 2, num+1) plt.title("åå¾ %d" % (num+1), fontproperties=myfont) plt.plot(x, y, style_list[num]) # å¾å½¢æ¾ç¤º plt.show() return # subplot_plot() def bar_plot(): """ bar plot """ # çææµè¯æ°æ® means_men = (20, 35, 30, 35, 27) means_women = (25, 32, 34, 20, 25) # 设置æ é¢ plt.title("æ±ç¶å¾", fontproperties=myfont) # 设置ç¸å ³åæ° index = np.arange(len(means_men)) bar_width = 0.35 # ç»æ±ç¶å¾ plt.bar(index, means_men, width=bar_width, alpha=0.2, color="b", label="ç·ç") plt.bar(index+bar_width, means_women, width=bar_width, alpha=0.8, color="r", label="女ç") plt.legend(loc="upper right", prop=myfont, shadow=True) # 设置æ±ç¶å¾æ 示 for x, y in zip(index, means_men): plt.text(x, y+0.3, y, ha="center", va="bottom") for x, y in zip(index, means_women): plt.text(x+bar_width, y+0.3, y, ha="center", va="bottom") # 设置å»åº¦èå´/åæ è½´åç§°ç plt.ylim(0, 45) plt.xlabel("åç»Group", fontproperties=myfont) plt.ylabel("å¾åScores", fontproperties=myfont) plt.xticks(index+(bar_width/2), ("Aç»", "Bç»", "Cç»", "Dç»", "Eç»"), fontproperties=myfont) # å¾å½¢æ¾ç¤º plt.show() return # bar_plot() def barh_plot(): """ barh plot """ # çææµè¯æ°æ® means_men = (20, 35, 30, 35, 27) means_women = (25, 32, 34, 20, 25) # 设置æ é¢ plt.title("æ¨ªåæ±ç¶å¾", fontproperties=myfont) # 设置ç¸å ³åæ° index = np.arange(len(means_men)) bar_height = 0.35 # ç»æ±ç¶å¾(æ°´å¹³æ¹å) plt.barh(index, means_men, height=bar_height, alpha=0.2, color="b", label="Men") plt.barh(index+bar_height, means_women, height=bar_height, alpha=0.8, color="r", label="Women") plt.legend(loc="upper right", shadow=True) # 设置æ±ç¶å¾æ 示 for x, y in zip(index, means_men): plt.text(y+0.3, x, y, ha="left", va="center") for x, y in zip(index, means_women): plt.text(y+0.3, x+bar_height, y, ha="left", va="center") # 设置å»åº¦èå´/åæ è½´åç§°ç plt.xlim(0, 45) plt.xlabel("Scores") plt.ylabel("Group") plt.yticks(index+(bar_height/2), ("A", "B", "C", "D", "E")) # å¾å½¢æ¾ç¤º plt.show() return # barh_plot() def bar_advanced_plot(): """ bar advanced plot """ # çææµè¯æ°æ® means_men = np.array((20, 35, 30, 35, 27, 25, 32, 34, 20, 25)) means_women = np.array((25, 32, 34, 20, 25, 20, 35, 30, 35, 27)) # 设置æ é¢ plt.title("é«çº§æ±ç¶å¾", fontproperties=myfont) # 设置ç¸å ³åæ° index = np.arange(len(means_men)) bar_width = 0.8 # ç»æ±ç¶å¾(两ç§:X轴以ä¸/X轴以ä¸) plt.bar(index, means_men, width=bar_width, alpha=0.4, color="b", label="Men") plt.bar(index, -means_women, width=bar_width, alpha=0.4, color="r", label="Women") # ç»æçº¿å¾(两ç§,åæ±ç¶å¾å¯¹åº) plt.plot(index, means_men, marker="o", linestyle="-", color="r", label="Men line") plt.plot(index, -means_women, marker=".", linestyle="--", color="b", label="Women line") # 设置å¾å½¢æ 示(两ç§,åæ±ç¶å¾å¯¹åº) for x, y in zip(index, means_men): plt.text(x, y+1, y, ha="center", va="bottom") for x, y in zip(index, means_women): plt.text(x, -y-1, y, ha="center", va="top") # 设置Yè½´åå¾ä¾ä½ç½® plt.ylim(-45, 80) plt.legend(loc="upper left", shadow=True) # å¾å½¢æ¾ç¤º plt.show() return # bar_advanced_plot() def table_plot(): """ table plot """ # çææµè¯æ°æ® data = np.array([ [1, 4, 2, 5, 2], [2, 1, 1, 3, 6], [5, 3, 6, 4, 1] ]) # 设置æ é¢ plt.title("屿¬¡æ±ç¶å¾", fontproperties=myfont) # 设置ç¸å ³åæ° index = np.arange(len(data[0])) color_index = ["r", "g", "b"] # 声æåºé¨ä½ç½® bottom = np.array([0, 0, 0, 0, 0]) # 便¬¡ç»å¾,å¹¶æ´æ°åºé¨ä½ç½® for i in range(len(data)): plt.bar(index, data[i], width=0.5, color=color_index[i], bottom=bottom, alpha=0.7, label="æ ç¾ %d" % i) bottom += data[i] # 设置å¾ä¾ä½ç½® plt.legend(loc="upper left", prop=myfont, shadow=True) # å¾å½¢æ¾ç¤º plt.show() return # table_plot() def histograms_plot(): """ histograms plot """ # çææµè¯æ°æ® mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # 设置æ é¢ plt.title("ç´æ¹å¾", fontproperties=myfont) # ç»ç´æ¹å¾, å¹¶è¿åç¸å ³ç»æ n, bins, patches = plt.hist(x, bins=50, normed=1, cumulative=False, color="green", alpha=0.6, label="ç´æ¹å¾") # æ ¹æ®ç´æ¹å¾è¿åçç»æ, ç»æçº¿å¾ y = mlab.normpdf(bins, mu, sigma) plt.plot(bins, y, "r--", label="线æ¡") # 设置å¾ä¾ä½ç½® plt.legend(loc="upper left", prop=myfont, shadow=True) # å¾å½¢æ¾ç¤º plt.show() return # histograms_plot() def pie_plot(): """ pie plot """ # çææµè¯æ°æ® sizes = [15, 30, 45, 10] labels = ["Frogs", "䏿", "Dogs", "Logs"] colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"] # 设置æ é¢ plt.title("饼å¾", fontproperties=myfont) # 设置çªåºåæ° explode = [0, 0.05, 0, 0] # ç»é¥¼ç¶å¾ patches, l_text, p_text = plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct="%1.1f%%", shadow=True, startangle=90) for text in l_text: text.set_fontproperties(myfont) plt.axis("equal") # å¾å½¢æ¾ç¤º plt.show() return # pie_plot() def scatter_plot(): """ scatter plot """ # çææµè¯æ°æ® point_count = 1000 x_index = np.random.random(point_count) y_index = np.random.random(point_count) # 设置æ é¢ plt.title("æ£ç¹å¾", fontproperties=myfont) # 设置ç¸å ³åæ° color_list = np.random.random(point_count) scale_list = np.random.random(point_count) * 100 # ç»æ£ç¹å¾ plt.scatter(x_index, y_index, s=scale_list, c=color_list, marker="o") # å¾å½¢æ¾ç¤º plt.show() return # scatter_plot() def fill_plot(): """ fill plot """ # çææµè¯æ°æ® x = np.linspace(-2*np.pi, 2*np.pi, 1000, endpoint=True) y = np.sin(x) # 设置æ é¢ plt.title("å¡«å å¾", fontproperties=myfont) # ç»å¾ plt.plot(x, y, color="blue", alpha=1.00) # å¡«å å¾å½¢, plt.fill_between(x, y1, y2, where=None, *kwargs) plt.fill_between(x, 0, y, where=(y > 0), color="blue", alpha=0.25) plt.fill_between(x, 0, y, where=(y < 0), color="red", alpha=0.25) # å¾å½¢æ¾ç¤º plt.show() return # fill_plot() def radar_plot(): """ radar plot """ # çææµè¯æ°æ® labels = np.array(["Aç»", "Bç»", "Cç»", "Dç»", "Eç»", "Fç»"]) data = np.array([68, 83, 90, 77, 89, 73]) theta = np.linspace(0, 2*np.pi, len(data), endpoint=False) # æ°æ®é¢å¤ç data = np.concatenate((data, [data[0]])) theta = np.concatenate((theta, [theta[0]])) # ç»å¾æ¹å¼ plt.subplot(111, polar=True) plt.title("é·è¾¾å¾", fontproperties=myfont) # 设置"theta grid"/"radar grid" plt.thetagrids(theta*(180/np.pi), labels=labels, fontproperties=myfont) plt.rgrids(np.arange(20, 100, 20), labels=np.arange(20, 100, 20), angle=0) plt.ylim(0, 100) # ç»é·è¾¾å¾,å¹¶å¡«å é·è¾¾å¾å é¨åºå plt.plot(theta, data, "bo-", linewidth=2) plt.fill(theta, data, color="red", alpha=0.25) # å¾å½¢æ¾ç¤º plt.show() return # radar_plot() def three_dimension_scatter(): """ 3d scatter plot """ # çææµè¯æ°æ® x = np.random.random(100) y = np.random.random(100) z = np.random.random(100) color = np.random.random(100) scale = np.random.random(100) * 100 # çæç»å¸(两ç§å½¢å¼) fig = plt.figure() fig.suptitle("ä¸ç»´æ£ç¹å¾", fontproperties=myfont) # ax = fig.gca(projection="3d") ax = fig.add_subplot(111, projection="3d") # ç»ä¸ç»´æ£ç¹å¾ ax.scatter(x, y, z, s=scale, c=color, marker=".") # è®¾ç½®åæ è½´å¾æ ax.set_xlabel("X Label") ax.set_ylabel("Y Label") ax.set_zlabel("Z Label") # è®¾ç½®åæ è½´èå´ ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_zlim(0, 1) # å¾å½¢æ¾ç¤º plt.show() return # three_dimension_scatter() def three_dimension_line(): """ 3d line plot """ # çææµè¯æ°æ® x = np.linspace(0, 1, 1000) y = np.linspace(0, 1, 1000) z = np.sin(x * 2 * np.pi) / (y + 0.1) # çæç»å¸(两ç§å½¢å¼) fig = plt.figure() ax = fig.gca(projection="3d", title="plot title") # ax = fig.add_subplot(111, projection="3d", title="plot title") # ç»ä¸ç»´æçº¿å¾ ax.plot(x, y, z, color="red", linestyle="-") # è®¾ç½®åæ è½´å¾æ ax.set_xlabel("X Label") ax.set_ylabel("Y Label") ax.set_zlabel("Z Label") # å¾å½¢æ¾ç¤º plt.show() return # three_dimension_line() def three_dimension_bar(): """ 3d bar plot """ # çææµè¯æ°æ®(ä½ç½®æ°æ®) xpos = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ypos = [2, 3, 4, 5, 1, 6, 2, 1, 7, 2] zpos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # çææµè¯æ°æ®(æ±å½¢åæ°) dx = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] dy = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # çæç»å¸(两ç§å½¢å¼) fig = plt.figure() ax = fig.gca(projection="3d", title="plot title") # ç»ä¸ç»´æ±ç¶å¾ ax.bar3d(xpos, ypos, zpos, dx, dy, dz, alpha=0.5) # è®¾ç½®åæ è½´å¾æ ax.set_xlabel("X Label") ax.set_ylabel("Y Label") ax.set_zlabel("Z Label") # å¾å½¢æ¾ç¤º plt.show() return # three_dimension_bar()