Python--matplotlib Drawing visualization practiced hand--line chart/bar chart

Source: Internet
Author: User
Tags jupyter notebook

Recently learn matplotlib mapping visualization, feel more knowledge points, while learning side record.

For data visualization, personal advice jupyter Notebook.

1. First guide the package, set the environment

import pandas as pdimport numpy as npimport sysreload(sys)sys.setdefaultencoding(‘utf-8‘)import matplotlib.pyplot as plt%matplotlib inline #使图片内嵌交互环境显示plt.rcParams[‘font.sans-serif‘]=[‘SimHei‘] #用来正常显示中文标签plt.rcParams[‘axes.unicode_minus‘]=False #用来正常显示负号

2. Read the data and display

data_every_month = pd.read_csv(‘data_every_month.txt‘)data_every_month

3. Draw a Line chart

y = data_every_month[‘nums‘].T.valuesx = range(0,len(y))plt.figure(figsize=(10, 6))plt.plot(x,y,‘‘)  plt.xticks((0,20,40,60,80,100,120),(‘200504‘,‘200912‘,‘201108‘,‘201306‘,‘201502‘,‘201610‘,‘‘))plt.xlabel(‘年月‘)plt.ylabel(‘XX事件数‘)plt.title(‘每月XX事件数‘)plt.show()

4. Take the fragment data, the same picture two lines to distinguish

y1=y[79:91]y2=y[91:102]x1=range(0,len(y1))x2=range(0,len(y2))plt.figure(figsize=(10, 6))plt.plot(x1,y1,‘‘,label="2015年")plt.plot(x2,y2,‘‘,label="2016年")plt.title(‘2015-2016年月XX事件数‘)plt.legend(loc=‘upper right‘)plt.xticks((0,2,4,6,8,10),(‘1月‘,‘3月‘,‘5月‘,‘7月‘,‘9月‘,‘11月‘))plt.xlabel(‘月份‘)plt.ylabel(‘XX事件数‘)plt.grid(x1)plt.show()

5. Read the hourly frequency data, draw the overlapping bar chart

data_hour2015 = pd.read_csv(‘data_hour2015.txt‘)data_hour2016 = pd.read_csv(‘data_hour2016.txt‘)plt.figure(figsize=(10, 6))data_hour2015[‘nums‘].T.plot.bar(color=‘g‘,alpha=0.6,label=‘2015年‘)data_hour2016[‘nums‘].T.plot.bar(color=‘r‘,alpha=0.4,label=‘2016年‘)plt.xlabel(‘小时‘)plt.ylabel(‘XX事件数量‘)plt.title(‘XX事件数小时分布‘)plt.legend(loc=‘upper right‘)plt.show()

6. Read Zhou Pin Number data, draw non-overlapping bar chart

data_week2015 = pd.read_csv(‘data_week2015.txt‘)[‘nums‘].T.valuesdata_week2016 = pd.read_csv(‘data_week2016.txt‘)[‘nums‘].T.valuesplt.figure(figsize=(10, 6))xweek=range(0,len(data_week2015))xweek1=[i+0.3 for i in xweek]plt.bar(xweek,data_week2015,color=‘g‘,width = .3,alpha=0.6,label=‘2015年‘)plt.bar(xweek1,data_week2016,color=‘r‘,width = .3,alpha=0.4,label=‘2016年‘)plt.xlabel(‘周‘)plt.ylabel(‘XX事件数量‘)plt.title(‘XX事件数周分布‘)plt.legend(loc=‘upper right‘)plt.xticks(range(0,7),[‘星期日‘,‘星期一‘,‘星期二‘,‘星期三‘,‘星期四‘,‘星期五‘,‘星期六‘])plt.show()

7. Read category frequency data draw horizontal bar chart

data_bar = pd.read_csv(‘data_bar.txt‘)label = data_bar[‘wfxw‘].T.valuesxtop = data_bar[‘nums‘].T.valuesidx = np.arange(len(xtop))fig = plt.figure(figsize=(12,12))plt.barh(idx, xtop, color=‘b‘,alpha=0.6)plt.yticks(idx+0.4,label)plt.grid(axis=‘x‘)plt.xlabel(‘XX事件次数‘)plt.ylabel(‘XX事件名称‘)plt.title(‘2015.1-2016.11月XX事件排行榜‘)plt.show()

Python--matplotlib Drawing visualization practiced hand--line chart/bar chart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.