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())描述的是线的参数,效果如图所示: