数据可视化库-Matplotlib基本操作

Posted mjerry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据可视化库-Matplotlib基本操作相关的知识,希望对你有一定的参考价值。

python-3.7     pycharm  matplotlib 2.2.3

 

"""
    数据可视化库-Matplotlib
    时间:2018912 0012
"""
import numpy as np
import pandas as pd
import matplotlib  as mpl
import matplotlib.pyplot as plt

print("""
----------------------------日期格式转换-------------------------------------
--------------------------------------pd.to_datetime(Series)----------------------------------------
""")
unrate = pd.read_csv("UNRATE.csv")
unrate["DATE"] = pd.to_datetime(unrate[DATE])
print(unrate.head(12))

print("""
----------------------------数据可视化库-------------------------------------
--------------------------------------matplotlib.pyplot(X轴,Y轴)----------------------------------------
""")
# 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("""
----------------------------数据可视化库-------------------------------------
--------------------------------------plt.add_subplot(行数,列数,索引)----------------------------------------
""")
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("""
----------------------------数据可视化库-------------------------------------
--------------------------------------一幅图画多套数据----------------------------------------
""")
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("""
----------------------------数据可视化库-------------------------------------
--------------------------------------柱形图ax.bar(位置,高度,宽度)----------------------------------------
""")
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("""
----------------------------数据可视化库-------------------------------------
--------------------------------------散点图ax.scatter(x坐标,y坐标)----------------------------
""")
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("""
----------------------------数据可视化库-------------------------------------
---------------------------范围的柱形图ax.hist(数据,range(范围低,范围高),bins = 柱形图范围个数)--------------
""")
"""
    set_ylim(0,50)设置y轴区间
    ax.boxplot(X,Y)  画出四分图
    set_visible(False) 不显示XY轴
"""

 

 

运行结果:

技术分享图片

技术分享图片

 

技术分享图片

技术分享图片

技术分享图片

技术分享图片

以上是关于数据可视化库-Matplotlib基本操作的主要内容,如果未能解决你的问题,请参考以下文章

AI常用框架和工具丨3. 可视化库Matplotlib

Python数据可视化库-Matplotlib

数据可视化入门 | 10张图搞定matplotlib基础操作

Python中数据可视化经典库都有哪些?

Mooc数据分析-02数据展示

Python中除了matplotlib外还都有哪些数据可视化的库