Python可视化-matplotlib用法详解(一)

一、折线图绘制

import pandas as pd
s='./../../data//unrate.csv'
unrate = pd.read_csv(s)
# 时间格式转换,
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
print(unrate.head(12))
         DATE  VALUE
0  1948-01-01    3.4
1  1948-02-01    3.8
2  1948-03-01    4.0
3  1948-04-01    3.9
4  1948-05-01    3.5
5  1948-06-01    3.6
6  1948-07-01    3.6
7  1948-08-01    3.9
8  1948-09-01    3.8
9  1948-10-01    3.7
10 1948-11-01    3.8
11 1948-12-01    4.0
import matplotlib.pyplot as plt
plt.plot()
plt.show()

在这里插入图片描述

first_twelve = unrate[0:12]
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.show()

在这里插入图片描述

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45)  # 指定角度,
#print help(plt.xticks)
plt.show()

在这里插入图片描述

plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=90)
plt.xlabel('Month')   # 横轴
plt.ylabel('Unemployment Rate') # 纵轴
plt.title('Monthly Unemployment Trends, 1948')  # 标题
plt.show()

在这里插入图片描述

二、漏斗图

# https://blog.youkuaiyun.com/yuetaope/article/details/123111188
from plotly import graph_objects as go
from plotly import io as pio

fig = go.Figure()

fig.add_trace(go.Funnel(
    name = 'Montreal',
    y = ["浏览", "下载", "潜在客户", "询价"],
    x = [120, 60, 30, 20],
    textinfo = "value+percent initial"))

fig.add_trace(go.Funnel(
    name = 'Toronto',
    orientation = "h",
    y = ["浏览", "下载", "潜在客户", "询价", "要约"],
    x = [100, 60, 40, 30, 20],
    textposition = "inside",
    textinfo = "value+percent previous"))

fig.add_trace(go.Funnel(
    name = 'Vancouver',
    orientation = "h",
    y = ["浏览", "下载", "潜在客户", "询价", "要约", "成交"],
    x = [90, 70, 50, 30, 10, 5],
    textposition = "outside",
    textinfo = "value+percent total"))

fig.show()
pio.write_image(fig, "1.png")

在这里插入图片描述

三、柱状图

import pandas as pd
reviews = pd.read_csv('../../data/fandango_scores.csv')
cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
norm_reviews = reviews[cols]
print(norm_reviews[:1])
                             FILM  RT_user_norm  Metacritic_user_nom  \
0  Avengers: Age of Ultron (2015)           4.3                 3.55   

   IMDB_norm  Fandango_Ratingvalue  Fandango_Stars  
0        3.9                   4.5             5.0  
import matplotlib.pyplot as plt
from numpy import arange
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']

bar_heights = norm_reviews.loc[0, num_cols].values  #高度
print (bar_heights)
bar_positions = arange(5) + 0.75            # 柱形图的位置
print (bar_positions)                        
fig,ax = plt.subplots()       #  ax是轴,fig是控制图的样子                 
ax.bar(bar_positions, bar_heights, 0.5)     
plt.show()
[4.3 3.55 3.9 4.5 5.0]
[0.75 1.75 2.75 3.75 4.75]

在这里插入图片描述

num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews.loc[0, num_cols].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)
fig, ax = plt.subplots()

ax.bar(bar_positions, bar_heights, 0.5)
ax.set_xticks(tick_positions)
ax.set_xticklabels(num_cols, rotation=45)

ax.set_xlabel('Rating Source')
ax.set_ylabel('Average Rating')
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
from numpy import arange
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']

bar_widths = norm_reviews.loc[0, num_cols].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)
fig, ax = plt.subplots()

ax.barh(bar_positions, bar_widths, 0.5)  # 横着的

ax.set_yticks(tick_positions)
ax.set_yticklabels(num_cols)
ax.set_ylabel('Rating Source')
ax.set_xlabel('Average Rating')
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
plt.show()

在这里插入图片描述

四、散点图

#Let's look at a plot that can help us visualize many points.
# 散点图
fig, ax = plt.subplots()
ax.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])
ax.set_xlabel('Fandango')
ax.set_ylabel('Rotten Tomatoes')
plt.show()

在这里插入图片描述

#Switching Axes
fig = plt.figure(figsize=(5,10))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])
ax1.set_xlabel('Fandango')
ax1.set_ylabel('Rotten Tomatoes')
ax2.scatter(norm_reviews['RT_user_norm'], norm_reviews['Fandango_Ratingvalue'])
ax2.set_xlabel('Rotten Tomatoes')
ax2.set_ylabel('Fandango')
plt.show()

在这里插入图片描述

参考资料

  • https://blog.youkuaiyun.com/qq_36439087/article/details/121527402
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值