import as plt

时间:2025-04-08 08:47:45
#!/usr/bin/env python #-*- coding: utf-8 -*- import math import random import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpathes from wrTXT import ReadDataTxt def test_01(): x = np.linspace(0, 10, 40) plt.figure(1) #创建画板1 plt.subplot(221) #进入221区域作图 # (x, (x), 'g', label="sin(x)", linewidth = 2.5 ) # green linewidth 2.5 plt.plot(x, np.sin(x), color='#2DCCD3', label="sin(x)", linewidth = 2.5, marker='^', linestyle='--') # green linewidth 2.5 plt.legend(loc="upper right") # 设置label为位置,显示在右上角 plt.axhline(y = 0.8, ls='--', c='r', linewidth = 2.5) #绘制平行于x轴y=0.8的水平参考线 plt.ylim(-1.5, 1.5) # 绘制垂直于x轴x<4 and x>6的参考区域,以及y轴y<0.2 and y>-0.2的参考区域 plt.axvspan(xmin=4, xmax=6, facecolor='r', alpha=0.3) # 垂直x轴 plt.axhspan(ymin=-0.2, ymax=0.2, facecolor='y', alpha=0.3); # 垂直y轴 # 添加注释文字sin(x) plt.text(8.0, 1.5, 'sin(x)', weight='bold', color='g', fontsize=15) # 8.0, 1.5 is x, y # 用箭头标出第一个峰值 plt.annotate('maximum',xy=(np.pi/2, 1),xytext=(np.pi/2+1, 1), weight='bold', color='r', arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r'), fontsize=15) plt.title('BUAA 01', fontsize=20) # 指定221区域的图的标题 plt.axis('equal') # 指定221区域的 x, y比例为一比一 plt.xlabel('variable x', fontsize=15) # 指定x坐标轴标题,可设置字体大小 plt.ylabel('value y', fontsize=15) # 指定y坐标轴标题,可设置字体大小 plt.grid() # 显示网格 plt.subplot(222) #进入222区域作图 plt.plot(x, np.sin(x), 'r-o', label="sin x") # red -o plt.legend(loc="upper right") # 设置label为位置,显示在右上角 plt.title('BUAA 02') # 指定111区域的图的标题 plt.axis('equal') # 指定111区域的 x, y比例为一比一 plt.grid() plt.subplot(223) #进入223区域作图 plt.scatter(x, np.sin(x)) plt.title('BUAA 03') # 指定111区域的图的标题 plt.axis('equal') # 指定111区域的 x, y比例为一比一 plt.grid() plt.subplot(224) #进入224区域作图 plt.plot(x, np.sin(x), 'g--', linewidth = 2.5) # green linewidth 2.5 plt.title('BUAA 04') # 指定111区域的图的标题 plt.axis('equal') # 指定111区域的 x, y比例为一比一 plt.grid() plt.show() # 用饼图的面积及颜色展示一组4维数据 def test_02(): plt.figure(1) plt.subplot(111) rng = np.random.RandomState(0) x = rng.randn(100) y = rng.randn(100) colors = rng.rand(100) sizes = 1000 * rng.rand(100) plt.scatter(x, y, c=colors, s=sizes, alpha=0.3, cmap='viridis') plt.colorbar(); # 展示色阶 plt.show() # 柱状图 def test_03(): plt.figure(1) plt.subplot(111) x = [1,2,3,4,5,6,7,8] y = [3,1,4,5,8,9,7,2] label = ['A','B','C','D','E','F','G','H'] plt.bar(x, y, tick_label = label) plt.show() if __name__ == "__main__": test_01()