数据可视化库-Matplotlib基本操作
python-3.7 pycharm matplotlib 2.2.3
""" 数据可视化库-Matplotlib 时间:2018\9\12 0012 """ import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt print("""\n----------------------------日期格式转换------------------------------------- --------------------------------------pd.to_datetime(Series)----------------------------------------\n""") unrate = pd.read_csv("UNRATE.csv") unrate["DATE"] = pd.to_datetime(unrate['DATE']) print(unrate.head(12)) print("""\n----------------------------数据可视化库------------------------------------- --------------------------------------matplotlib.pyplot(X轴,Y轴)----------------------------------------\n""") # plt.plot() # 画图操作 # plt.show() # 显示出来 first_twelve = unrate[0:12] plt.plot(first_twelve['DATE'], first_twelve['VALUE']) plt.xticks(rotation = 45) # X数字为45° plt.xlabel('Month') # X轴标签 plt.ylabel('Unemployment Rate') # Y轴标签 plt.title('Monthly Unemployment Trends 1948') # 标题 plt.show() print("""\n----------------------------数据可视化库------------------------------------- --------------------------------------plt.add_subplot(行数,列数,索引)----------------------------------------\n""") fig = plt.figure() # figuresize = (行,列) ax1 = fig.add_subplot(4, 3, 1) ax2 = fig.add_subplot(4, 3, 2) ax3 = fig.add_subplot(4, 3, 3) ax6 = fig.add_subplot(4, 3, 6) ax1.plot(np.random.randint(1, 5, 5), np.arange(5)) ax2.plot(np.arange(10) * 3, np.arange(10)) plt.show() print("""\n----------------------------数据可视化库------------------------------------- --------------------------------------一幅图画多套数据----------------------------------------\n""") unrate['MONTH'] = unrate['DATE'].dt.month unrate['MONTH'] = unrate['DATE'].dt.month fig = plt.figure(figsize = (6, 3)) plt.plot(unrate[0:12]['MONTH'], unrate[0:12]['VALUE'], c = 'red') plt.plot(unrate[12:24]['MONTH'], unrate[12:24]['VALUE'], c = 'blue') plt.show() # fig = plt.figure(figsize = (10, 6)) colors = ['red', 'blue', 'green', 'orange', 'black'] for i in range(5): start_index = i * 12 end_index = (i + 1) * 12 subset = unrate[start_index:end_index] label = str(1948 + i) plt.plot(subset['MONTH'], subset['VALUE'], c = colors[i], label = label) plt.legend(loc = 'best') plt.xlabel('Month') # X轴标签 plt.ylabel('Unemployment Rate') # Y轴标签 plt.title('Monthly Unemployment Trends 1948-1952') # 标题 plt.show() print("""\n----------------------------数据可视化库------------------------------------- --------------------------------------柱形图ax.bar(位置,高度,宽度)----------------------------------------\n""") reviews = pd.read_csv('fandango_score_comparison.csv') cols = ['FILM', 'RT_user_norm', 'Metacritic_norm', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'] norm_reviews = reviews[cols] num_cols = ['RT_user_norm', 'Metacritic_norm', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'] bar_heights = norm_reviews.loc[0, num_cols].values # 第0行,第num_cols列 print(bar_heights) bar_prosions = np.arange(5) + 1 tick_prosions = range(1, 6) print(bar_prosions) ax = plt.subplot() ax.bar(bar_prosions, bar_heights, 0.3) # ax.barh(bar_prosions, bar_heights, 0.3) #纵横转换 ax.set_xticks(tick_prosions) ax.set_xticklabels(num_cols, rotation = 45) ax.set_xlabel("Rating Source") ax.set_ylabel("Average Rating") ax.set_title('Average User Rating For Averages: Age of Ultron(2015)') plt.show() print("""\n----------------------------数据可视化库------------------------------------- --------------------------------------散点图ax.scatter(x坐标,y坐标)----------------------------\n""") ax = plt.subplot() ax.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm']) ax.set_xlabel("Fandango") ax.set_ylabel("RottenTomatoes") plt.show() print("""\n----------------------------数据可视化库------------------------------------- ---------------------------范围的柱形图ax.hist(数据,range(范围低,范围高),bins = 柱形图范围个数)--------------\n""") """ set_ylim(0,50)设置y轴区间 ax.boxplot(X,Y) 画出四分图 set_visible(False) 不显示XY轴 """
运行结果: