数据可视化-matplotlib运用以及Plotly的使用(附代码)

1.先从绘制一些简单的图开始

绘制简单的折线图

演示

import matplotlib.pyplot as plt

squares=[1,4,9,16,25]

plt.plot(squares)

运行结果

在这里插入图片描述

修改标签文字和线条粗细

续上

import matplotlib.pyplot as plt

squares=[1,4,9,16,25]

plt.plot(squares,linewidth=5)#线宽

#设标题,并给坐标抽加上标签
plt.title("squares numbers",fontsize=24)#标题内容和大小
plt.xlabel("value",fontsize=21)
plt.ylabel("Square of Value",fontsize=21)

plt.tick_params(axis='both',labelsize=14)#刻度大小,both表示xy轴

plt.show()

效果:
在这里插入图片描述

矫正上述图形(二维输入)

import matplotlib.pyplot as plt

squares=[1,4,9,16,25]
input_value=[1,2,3,4,5]
plt.plot(input_value,squares,linewidth=5)#线宽

#设标题,并给坐标抽加上标签
plt.title("squares numbers",fontsize=24)#标题内容和大小
plt.xlabel("value",fontsize=21)
plt.ylabel("Square of Value",fontsize=21)

plt.tick_params(axis='both',labelsize=14)#刻度大小,both表示xy轴

plt.show()

在这里插入图片描述

2.散点图scatter

续上:

import matplotlib.pyplot as plt

squares=[1,4,9,16,25]
input_value=[1,2,3,4,5]
plt.scatter(input_value,squares,s=20)#s描述点大小比例

#设标题,并给坐标抽加上标签
plt.title("squares numbers",fontsize=24)#标题内容和大小
plt.xlabel("value",fontsize=21)
plt.ylabel("Square of Value",fontsize=21)

plt.tick_params(axis='both',labelsize=14)#刻度大小,both表示xy轴

plt.show()

效果:

在这里插入图片描述

删除数据点的轮廓

import matplotlib.pyplot as plt

squares=[1,4,9,16,25]
input_value=[1,2,3,4,5]
plt.scatter(input_value,squares,s=20,edgecolors='none')#线宽

#设标题,并给坐标抽加上标签
plt.title("squares numbers",fontsize=24)#标题内容和大小
plt.xlabel("value",fontsize=21)
plt.ylabel("Square of Value",fontsize=21)

plt.tick_params(axis='both',labelsize=14)#刻度大小,both表示xy轴

plt.show()

效果比对图:(点数多,且s比较大的时候比较明显,消除轮廓主要作用是为了防止点与点之间粘连在一起)
在这里插入图片描述

3.自定义颜色

要修改数据点颜色,需要在scatter传递参数c,有以下两种表示方法:

直接赋颜色

plt.scatter(x_values,y_values,c='red',edgecolor='none',s=40)

RGB自定义颜色

plt.scattle(x_values,y_values,c=(0,0,0.8),edgecolor='none',s=40)

颜色映射

import matplotlib.pyplot as plt

squares=[1,4,9,16,25]
input_value=[1,2,3,4,5]
plt.scatter(input_value,squares,c=squares,cmap=plt.cm.Blues,s=20)#s描述点大小比例
#
#设标题,并给坐标抽加上标签
plt.title("squares numbers",fontsize=24)#标题内容和大小
plt.xlabel("value",fontsize=21)
plt.ylabel("Square of Value",fontsize=21)

plt.tick_params(axis='both',labelsize=14)#刻度大小,both表示xy轴

plt.show()

在这里插入图片描述

4.自动保存图标方法

plt.savefig("--.png",bbox_inches='tight')

5.随机漫步(rw包)

6.其它图像表现方法plotly

散点图和线性图

import plotly as py
import plotly.graph_objs as go

# ----------pre def
pyplt = py.offline.plot

# ----------code
# Create random data with numpy
import numpy as np

N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5

# Create traces
trace0 = go.Scatter(
    x = random_x,
    y = random_y0,
    mode = 'markers', # 纯散点的绘图
    name = 'markers' # 曲线名称
)
trace1 = go.Scatter(
    x = random_x,
    y = random_y1,
    mode = 'lines+markers', # 散点+线的绘图
    name = 'lines+markers'
)
trace2 = go.Scatter(
    x = random_x,
    y = random_y2,
    mode = 'lines', # 线的绘图
    name = 'lines'
)

data = [trace0, trace1, trace2]
pyplt(data, filename='scatter_basic_demo.html',image='png')

在mode里面分别输入数据绘图格式:markers,lines 或者markers+lines会输出三种不同的模式,而name可以对曲线进行命名,效果如下:

在这里插入图片描述

样式设计

import plotly as py
import plotly.graph_objs as go

import numpy as np

# ----------pre def
pyplt = py.offline.plot

# ----------code
N = 500
x = np.random.randn(N)

trace0 = go.Scatter(
    x = np.random.randn(N),
    y = np.random.randn(N)+2,
    name = 'Above',
    mode = 'markers+lines',
    marker = dict(
        size = 10, # 设置点的宽度
        color = 'rgba(152, 0, 0, .8)', # 设置曲线的颜色
        line = dict(
            width = 2, # 设置线条的宽度
            color = 'rgb(0, 0, 0)' # 设置线条的颜色
        )
    )
)


trace1 = go.Scatter(
    x = np.random.randn(N),
    y = np.random.randn(N) - 2,
    name = 'Below',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(255, 182, 193, .9)',
        line = dict(
            width = 2,
        )
    )
)

data = [trace0, trace1]

layout = dict(title = 'Styled Scatter',
              yaxis = dict(zeroline = True), # 显示y轴的0刻度线
              xaxis = dict(zeroline = False) # 不显示x轴的0刻度线
             )

fig = dict(data=data, layout=layout)
pyplt(fig, filename='scatter_style.html',image='png')

这里size和第一个color设置的是点的大小及其颜色,width和第二个color(括在line=dict())描述的是线的参数,效果如图所示:

在这里插入图片描述

Scatter参数说明

未完,待更,matplotlib可以结合pandas读取数据集来整体对数据或者运行模型效果等具有更好的可视性,pygal画廊:http://www.pygal.org/.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值