实验目的
    1、了解 python 中的可视化库
    2、掌握 python 中 pandas 库、seaborn 库的使用
    3、掌握 python 中 bokeh 库、pyqtgraph 库的使用
    实验环境
    1、64 位电脑,8G 以上内存
    2、win10 系统
    3、matplotlib 和 pandas、seaborn、bokeh 和 pyqtgraph 库
    实验步骤:
    1. pandas 库的使用
    (1)安装
    pip install pandas
    (2)Series 的创建与输出

    1. from pandas import Series
    2. import pandas as pd
    3. s = Series([1,4,'ww','tt'])
    4. print(s)
    5. print(s.values)
    6. print(s.index)

    image.png
    (3)自定义索引
    列表的索引只能是从 0 开始的整数,Series 数据类型在默认情况下,其索引也是如此。不过,区别于
    列表的是 Series 可以自定义以下索引 s2,输出 s2,根据索引修改 s2

    1. from pandas import Series
    2. import pandas as pd
    3. s2 = Series(['wangxing','man',24],index=['name','sex','age'])
    4. print(s2)
    5. print(s2['name'])
    6. s2['name']='zhangsan'
    7. print(s2['name'])

    image.png
    (4) 另外一种定义 Series 的方法,观察输出结果

    1. from pandas import Series
    2. import pandas as pd
    3. sd = {'python':9000,'c++':9001,'c#':9000}
    4. s3 = Series(sd)
    5. print(s3)

    image.png
    (5) 定义 Series,省略 index

    1. from pandas import Series,DataFrame
    2. data = {"name":['google','baidu','yahoo'],"marks":[100,200,300],"price":[1,2,3]}
    3. f1 = DataFrame(data)
    4. print(f1) #观察输出结果

    image.png
    (6) 定义 Series,加上 index

    1. from pandas import Series,DataFrame
    2. data = {"name":['google','baidu','yahoo'],"marks":[100,200,300],"price":[1,2,3]}
    3. f1 = DataFrame(data)
    4. newdata = {'lang':{'first':'python','second':'java'},'price':{'first':5000,'second':2000}}
    5. f4 = DataFrame(newdata)
    6. print(f4)
    7. print(f4['lang']) #输出某一列
    8. print(f4.loc['first']) #输出某一行
    9. print(f4['lang']['first']) #输出某个值

    image.png
    (7)练习教材 P203,例 8-19,观察程序输出结果。
    8-19

    1. import pandas as pd
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
    5. s.plot()
    6. plt.show()

    image.png
    (8)练习教材 P205,例 8-20,观察程序输出结果。
    8-20

    1. from pandas import DataFrame,Series
    2. import pandas as pd
    3. import numpy as np
    4. import matplotlib.pyplot as plt
    5. df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
    6. columns=['A', 'B', 'C', 'D'],
    7. index=np.arange(0, 100, 10))
    8. df.plot()
    9. plt.show()

    image.png
    2. seaborn 库的使用
    (1)安装 seaborn
    (2)练习 p210 例 8-24
    8-24

    1. import numpy as np
    2. import pandas as pd
    3. import matplotlib.pyplot as plt
    4. import seaborn as sns
    5. sns.set_style("darkgrid")
    6. plt.plot(np.arange(10))
    7. plt.show()

    image.png
    3. bokeh 库的使用
    (1)安装
    (2)练习 p215 例 8-28
    8-28

    1. from bokeh.plotting import figure, output_file, show
    2. output_file("patch.html")
    3. p = figure(plot_width=400, plot_height=400)
    4. p.patch([1, 2, 3, 4, 5], [6, 7, 8, 9, 10], alpha=0.5, line_width=2)
    5. show(p)

    image.png
    (3)练习 p216 例 8-29、例 8-30
    8-29

    1. from bokeh.plotting import figure, output_file, show
    2. output_file("patch.html")
    3. p = figure(plot_width=400, plot_height=400)
    4. p.patch([1, 2, 3, 4, 5], [6, 8, 5, 3, 4], alpha=0.5, line_width=2)
    5. show(p)

    image.png
    8-30

    1. from bokeh.plotting import figure, output_file, show
    2. output_file("circle.html")
    3. p = figure(plot_width=400, plot_height=400)
    4. p.circle([1, 2, 3, 4, 5], [3, 4, 5, 6, 7], size=20, color="red", alpha=0.5)
    5. show(p)

    image.png
    (3)练习 p217 例 8-31
    8-31

    1. from bokeh.plotting import figure, output_file, show
    2. output_file("square.html")
    3. p = figure(plot_width=400, plot_height=400)
    4. p.square([1, 2, 3, 4, 5], [7, 8, 5, 3, 7], size=30, color="red", alpha=0.5)
    5. show(p)

    image.png
    4. pyqtgraph 库的使用
    (1)安装 pyqtgraph 和 PyQt5
    (2)练习 p220 例 8-35
    8-35

    import pyqtgraph as pg
    import numpy as np
    app=pg.mkQApp()
    x=np.random.random(10)
    pg.plot(x)
    app.exec_()
    

    image.png
    (3)练习 p221 例 8-36
    8-36

    import pyqtgraph as pg
    import numpy as np
    app=pg.mkQApp()
    x=np.linspace(0,1*np.pi,100)
    y=np.sin(x)
    pg.plot(x,y)
    app.exec_()
    

    image.png