python之matplotlib画图库学习绘制常用的图
来源:互联网 发布:ipad看图软件 编辑:程序博客网 时间:2024/06/05 20:11
github地址:https://github.com/xiaoping1988/ping-ml
导入相关类
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inlineplt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签plt.rcParams['axes.unicode_minus']=False #用来正常显示负号#有中文出现的情况,需要u'内容'#需要安装simhei.ttf字体,装完之后要重启这个notebook文件才行#参考https://www.zhihu.com/question/25404709/answer/170849168#指定图形的字体 font = {'color' : 'darkred', 'weight' : 'normal', 'size' : 16 }
画一个折线图
ur = pd.read_csv('unrate.csv')print(ur.dtypes)## 转成时间格式ur['DATE'] = pd.to_datetime(ur['DATE'])print(ur.dtypes)## 读取头部12个月数据ur12 = ur.loc[0:11]## x轴:DATE,y轴:VALUEplt.figure(figsize=(10,5))plt.plot(ur12['DATE'],ur12['VALUE'],label='value1') # 折线1plt.plot(ur12['DATE'],ur12['VALUE2'],label='value2') # 折线2plt.xticks(rotation=45) # 控制x轴数据旋转角度plt.xlabel(u'月份',fontdict=font)plt.ylabel(u'比率',fontdict=font)plt.title(u'比率趋势图',fontdict=font)plt.legend(loc=2) # 图例,loc是位置plt.show()
柱状图,纵向和横向的
ur = pd.read_csv('unrate.csv',index_col='DATE') # DATE列为索引print(ur.head())print(ur.dtypes)## 读取头部12个月数据,并转置,是月份为列名ur12 = ur.loc['1948/1/1':'1948/12/1'].Tprint(ur12.columns.tolist())print(ur12.head())cols = ur12.columns.tolist()bar_left = np.arange(len(cols)) ## 控制柱子的位置bar_width = 0.4 # 柱子宽度opacity = 0.4bar_height_value = ur12.loc['VALUE',cols].valuesbar_height_value2 = ur12.loc['VALUE2',cols].valuesprint(bar_height_value)print(bar_height_value2)fig = plt.figure(figsize=(10,10))as1 = fig.add_subplot(2,1,1) # 添加子图,位置在2行1列的第一个,纵向的as1.bar(bar_left-bar_width/2,bar_height_value,bar_width,alpha=opacity,label='value1') # 柱状1as1.bar(bar_left+bar_width/2,bar_height_value2,bar_width,alpha=opacity,label='value2') # 柱状2as1.set_xticks(bar_left) # 控制x轴数据旋转角度as1.set_xticklabels(cols,rotation=45) # 控制x轴数据旋转角度as1.set_xlabel(u'月份',fontdict=font)as1.set_ylabel(u'比率',fontdict=font)as1.set_title(u'比率趋势图',fontdict=font)as1.legend(loc=2) # 图例,loc是位置as2 = fig.add_subplot(2,1,2) # 添加子图,位置在2行1列的第二个,横向的as2.barh(bar_left-bar_width/2,bar_height_value,bar_width,alpha=opacity,label='value1') # 柱状1as2.barh(bar_left+bar_width/2,bar_height_value2,bar_width,alpha=opacity,label='value2') # 柱状2as2.set_yticks(bar_left) # 控制x轴数据旋转角度as2.set_yticklabels(cols) # 控制x轴数据旋转角度as2.set_ylabel(u'月份',fontdict=font)as2.set_xlabel(u'比率',fontdict=font)as2.set_title(u'比率趋势图',fontdict=font)as2.legend(loc=1) # 图例,loc是位置plt.show()
散点图
## 散点图fs = pd.read_csv('fandango_scores.csv')print(fs.columns.tolist())print(fs.head(1))plt.figure(figsize=(8,6))# x轴:Fandango_Ratingvalue,y轴:RT normplt.scatter(fs['Fandango_Ratingvalue'],fs['RT_user_norm']) # 散点1plt.scatter(fs['Fandango_Ratingvalue'],fs['RT_norm'],marker='x',c='r') # 散点2plt.xlabel('Fandango_Ratingvalue')plt.ylabel('RT norm')plt.title(u'散点图1')plt.show()
直方图
fs = pd.read_csv('fandango_scores.csv')plt.figure(figsize=(10,10))plt.hist(fs['Fandango_Ratingvalue'],range=(3,5),bins=10)plt.ylabel('Fandango_Ratingvalue')plt.show()
盒图,最大值、最小值、中位数、75位数、25位数
fs = pd.read_csv('fandango_scores.csv')plt.figure(figsize=(10,8))cols = ['Fandango_Ratingvalue','RT_norm','RT_user_norm']plt.boxplot(fs[cols].values)plt.xticks(np.arange(len(cols))+1,cols,rotation=90)plt.show()
阅读全文
0 0
- python之matplotlib画图库学习绘制常用的图
- python之seaborn画图库学习绘制常用的图
- Python学习之matplotlib 绘制折线图详解
- Python之Matplotlib画图方法
- python的画图模块matplotlib
- 用python的matplotlib画图
- python画图——matplotlib.plot 学习
- python画图matplotlib的Legend(显示图中的标签)
- python matplotlib 方程的绘制
- python matplotlib图像的绘制
- python 的 matplotlib画图 画曲线图
- Python matplotlib画图的中文显示问题
- 用python的matplotlib画图总结1
- 安装Python的画图工具matplotlib
- Python图表绘制库Matplotlib学习(一)显示窗口
- Matplotlib常用画图代码
- python之matplotlib学习
- python 数据可视化 matplotlib学习一:绘制简单的折线图
- 使用selenium爬取百度文库文章(动态)
- offer34--丑数
- spring boot中出现parent报错
- Vue之计算属性
- SpringBoot微服务之分布式跟踪系统(springboot+zipkin)
- python之matplotlib画图库学习绘制常用的图
- String类 (浅拷贝/深拷贝/写时拷贝)
- 实现轮胎印
- 微服务之分布式跟踪系统(springboot+zipkin+mysql)
- redis的安装
- UICC之UiccCard
- python循环输出三角形图案
- MyBatis初窥:HelloWorld
- 动态规划-再次理解最长上升子序列