一、Python绘图介绍
1.1python绘图库介绍
1.1.1Matplotlib库
matplotlib作为Python的基本绘图库,是Python中应用最广泛的绘图工具包之一,matplotlib能和其他很多库结合,如pandas等
1.1.2ggplot2其他库
包括ggplot2和seaborn,还有pyecharts库等都是第三方绘图库,可以优化Python图形,使得Python数据可视化结果更加美观
1.2绘图环境部署
1.2.1安装python包
C:\Users\asp>pip install numpy
C:\Users\asp>python -m pip install --upgrade pip
C:\Users\asp>pip install matplotlib
C:\Users\asp>pip install pandas
二、基本绘图入门
2.1绘制sin图
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
os.chdir(r'C:\34-Python绘图')
# 简单图形代码
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y)
plt.show()
2.2绘制坐标图-标签和图例
x = np.arange(0,1.1,0.01)
y = x**2
plt.figure(figsize=(9,9),dpi=80) #确定画布大小,dpi:图形分辨率
plt.title('lines') #添加标题
plt.xlabel('x1')
plt.ylabel('y')
plt.xlim((0,1)) # 确定x轴的范围
plt.ylim((0,1)) # 确定x轴的范围
plt.xticks([0,0.2,0.4,0.6,0.8,1]) #确定x轴的刻度
plt.yticks([0,0.2,0.4,0.6,0.8,1]) #确定y轴的刻度
plt.plot(x,y, label='y =x^2')
plt.legend(loc='best') # 图例
plt.show()
0,1.1,0.01) > x = np.arange(
2 > y = x**
9,9),dpi=80) #确定画布大小,dpi:图形分辨率 > plt.figure(figsize=(
'lines') #添加标题 > plt.title(
Text(0.5, 1.0, 'lines')
'x1') > plt.xlabel(
Text(0.5, 0, 'x1')
'y') > plt.ylabel(
Text(0, 0.5, 'y')
0,1)) # 确定x轴的范围 > plt.xlim((
(0, 1)
0,1)) # 确定x轴的范围 > plt.ylim((
(0, 1)
0,0.2,0.4,0.6,0.8,1]) #确定x轴的刻度 > plt.xticks([
0,0.2,0.4,0.6,0.8,1]) #确定y轴的刻度 > plt.yticks([
'y =x^2') > plt.plot(x,y, label=
[]
'best') # 图例 > plt.legend(loc=
> plt.show()
> x
array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,
0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,
0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32,
0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43,
0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65,
0.66, 0.67, 0.68, 0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76,
0.77, 0.78, 0.79, 0.8 , 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87,
0.88, 0.89, 0.9 , 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,
0.99, 1. , 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09])
> y
array([0.0000e+00, 1.0000e-04, 4.0000e-04, 9.0000e-04, 1.6000e-03,
2.5000e-03, 3.6000e-03, 4.9000e-03, 6.4000e-03, 8.1000e-03,
1.0000e-02, 1.2100e-02, 1.4400e-02, 1.6900e-02, 1.9600e-02,
2.2500e-02, 2.5600e-02, 2.8900e-02, 3.2400e-02, 3.6100e-02,
4.0000e-02, 4.4100e-02, 4.8400e-02, 5.2900e-02, 5.7600e-02,
6.2500e-02, 6.7600e-02, 7.2900e-02, 7.8400e-02, 8.4100e-02,
9.0000e-02, 9.6100e-02, 1.0240e-01, 1.0890e-01, 1.1560e-01,
1.2250e-01, 1.2960e-01, 1.3690e-01, 1.4440e-01, 1.5210e-01,
1.6000e-01, 1.6810e-01, 1.7640e-01, 1.8490e-01, 1.9360e-01,
2.0250e-01, 2.1160e-01, 2.2090e-01, 2.3040e-01, 2.4010e-01,
2.5000e-01, 2.6010e-01, 2.7040e-01, 2.8090e-01, 2.9160e-01,
3.0250e-01, 3.1360e-01, 3.2490e-01, 3.3640e-01, 3.4810e-01,
3.6000e-01, 3.7210e-01, 3.8440e-01, 3.9690e-01, 4.0960e-01,
4.2250e-01, 4.3560e-01, 4.4890e-01, 4.6240e-01, 4.7610e-01,
4.9000e-01, 5.0410e-01, 5.1840e-01, 5.3290e-01, 5.4760e-01,
5.6250e-01, 5.7760e-01, 5.9290e-01, 6.0840e-01, 6.2410e-01,
6.4000e-01, 6.5610e-01, 6.7240e-01, 6.8890e-01, 7.0560e-01,
7.2250e-01, 7.3960e-01, 7.5690e-01, 7.7440e-01, 7.9210e-01,
8.1000e-01, 8.2810e-01, 8.4640e-01, 8.6490e-01, 8.8360e-01,
9.0250e-01, 9.2160e-01, 9.4090e-01, 9.6040e-01, 9.8010e-01,
1.0000e+00, 1.0201e+00, 1.0404e+00, 1.0609e+00, 1.0816e+00,
1.1025e+00, 1.1236e+00, 1.1449e+00, 1.1664e+00, 1.1881e+00])
>
2.3增加元素
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y,ls=':',lw=2,label='x和y的关系')
plt.legend()
plt.show()
0,10,100) > x = np.linspace(
> y = np.sin(x)
':',lw=2,label='x和y的关系') > plt.plot(x,y,ls=
[]
> plt.legend()
> plt.show()
C:\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 21644 missing from current font.
font.set_text(s, 0.0, flags=flags)
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y,ls='-',lw=2,label='x和y的关系')
plt.legend()
plt.show()
实点线
2.4线条控制
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y,ls=':',lw=2,label='x和y的关系')
plt.legend(loc= 'upper center')
plt.show()
# ls- -函数线条风格(='-' 实线, '--' 虚线 ,'-.' 点划线 ,':' 实点线)
# lw 线条宽度
2.5线条调整
# 调整线条样式,宽度,形状和点
# marker线条上点的形状
# markersize点的大小
# c 颜色
# markeredgecolor点的边框色
# markerfacecolor点的填充色
x = np.linspace(0,10,100)
y = np.sin(x)
#plt.plot(x,y,ls=':',lw=2,marker='D',markersize=2,c= 'r',label='x和y的关系')
plt.plot(x,y,ls='--',lw=2,marker='s',markersize=2,c= 'red',markeredgecolor ='blue',markerfacecolor='black',label='x和y的关系')
plt.legend(loc= 'center')
plt.show()
2.6小技巧
2.6.1支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
2.7plot绘图模块
2.7.1基本介绍
matplotlib.plot是最常见的绘图的模块,语法如下:
plt.plot(x,y,ls=,lw=,c=,marker=,markersize=,markeredgecolor=,markerfacecolor, label=)
x: x轴上的数值
y: y轴上的数值
ls: 折线的风格(‘-‘, ’ --‘, ’ -.‘和':‘)
lw: 线条宽度
c: 颜色
marker: 线条上点的形状
markersize: 线条上点的大小
markeredgecolor: 点的边框色
markerfacecolor: 点的填充色
label: 文本标签
2.7.2线条风格参数
线条风格参数
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker
2.7.3颜色
cnames = {
'aliceblue': '#F0F8FF',
'antiquewhite': '#FAEBD7',
'aqua': '#00FFFF',
'aquamarine': '#7FFFD4',
'azure': '#F0FFFF',
'beige': '#F5F5DC',
'bisque': '#FFE4C4',
'black': '#000000',
'blanchedalmond': '#FFEBCD',
'blue': '#0000FF',
'blueviolet': '#8A2BE2',
'brown': '#A52A2A',
'burlywood': '#DEB887',
'cadetblue': '#5F9EA0',
'chartreuse': '#7FFF00',
'chocolate': '#D2691E',
…………
………………
'violet': '#EE82EE',
'wheat': '#F5DEB3',
'white': '#FFFFFF',
'whitesmoke': '#F5F5F5',
'yellow': '#FFFF00',
'yellowgreen': '#9ACD32'}
三、简单绘图例子
3.1安装对应的包
C:\Users\asp>pip install xlrd
3.2读取文件
文件内容
Date | Counts | Times |
2017/10/1 | 399 | 763 |
2017/10/2 | 126 | 345 |
2017/10/3 | 76 | 249 |
2017/10/4 | 59 | 182 |
2017/10/5 | 60 | 165 |
2017/10/6 | 59 | 332 |
2017/10/7 | 80 | 299 |
2017/10/8 | 801 | 1297 |
wechat = pd.read_excel('wechat.xlsx')
wechat.Date=pd.to_datetime(wechat.Date,format='%Y-%m-%d')
查看数据信息
wechat.info()
>>> wechat = pd.read_excel('wechat.xlsx')
wechat.Date=pd.to_datetime(wechat.Date,format='%Y-%m-%d')
Date Counts Times
0 2017-10-01 399 763
1 2017-10-02 126 345
2 2017-10-03 76 249
3 2017-10-04 59 182
4 2017-10-05 60 165
... ... ...
87 2017-12-27 1199 2282
88 2017-12-28 1833 2839
89 2017-12-29 1820 2992
90 2017-12-30 323 1096
91 2017-12-31 1894 2710
rows x 3 columns]
>>> wechat.info()
RangeIndex: 92 entries, 0 to 91
Data columns (total 3 columns):
Date 92 non-null datetime64[ns]
Counts 92 non-null int64
Times 92 non-null int64
dtypes: datetime64[ns](1), int64(2)
memory usage: 2.3 KB
3.3绘制单条折线图
# 绘制单条折线图
plt.plot(wechat.Date, # x轴数据
wechat.Counts, # y轴数据
linestyle = '-', # 折线类型
linewidth = 2, # 折线宽度
color = 'steelblue', # 折线颜色
marker = 'o', # 折线图中添加圆点
markersize = 6, # 点的大小
markeredgecolor='black', # 点的边框色
markerfacecolor='red') # 点的填充色
# 添加y轴标签
plt.ylabel('人数')
plt.xticks(rotation=45)
# 添加图形标题
plt.title('每天微信文章阅读人数趋势')
# 显示图形
plt.show()
3.4保存图形到文件
######保存图形
x = np.arange(0,1.1,0.01)
y = x**2
plt.figure(figsize=(9,9),dpi=80) #确定画图大小,dpi:图形分辨率
plt.title('lines') #添加标题
plt.xlabel('x')
plt.ylabel('y')
plt.xlim((0,1)) # 确定x轴的范围
plt.ylim((0,1)) # 确定x轴的范围
plt.xticks([0,0.2,0.4,0.6,0.8,1]) #确定x轴的刻度
plt.yticks([0,0.2,0.4,0.6,0.8,1]) #确定y轴的刻度
plt.plot(x,y,label='y =x^2')
plt.legend(loc='best')
plt.savefig(r'C:\34-Python绘图\\保存1.pdf')
plt.show()
Python绘图其实也很强大,以上是基础内容。
今天的分享就到这里了
欢迎点赞、在看、分享转发朋友圈 ^.^ 公众号还可以“设为星标”喔~
联系方式:微信号 coding_gene QQ 391399793 QQ群 338633443