Data Analysis--graphing (Python)

Source: Internet
Author: User

First, the basic settings

Import the related libraries

Import pandas as Pdimport NumPy as Npimport Matplotlib.pyplot as Pltimport Seaborn as Sns%matplotlib inline   #在ipython总 Show Chart

Chinese is not displayed by default, so you need to change the settings to display Chinese

#显示中文import matplotlib as Mpl mpl.rcparams[' font.sans-serif ') = [u ' simhei ']mpl.rcparams[' axes.unicode_minus '] = False

Setting Global variables

Mpl.rc (' font ', size=12)  #字体mpl. RC (' figure ', figsize= (8,6))  #图像大小mpl. RC (' Axes.spines ', right=false,top= False)  #设置右边上边的横线是否显示

Second, import the data, and view

data = Sns.load_dataset (' tips ') data.head ()
#字段含义分别是: Total consumption, tipping, sex, whether smoking, weekends, time, several people

 

Three, drawing (matplotlib)

1. Line chart

Fig,ax = Plt.subplots ()  #fig主要设置一些全局的变量, and Ax is primarily responsible for drawing ax.plot (data.index,data[' Total_bill ')) fig.set_size_inches ( 12,6) #重新设置大小plt. Title (' Line chart title ', fontsize=22)  #标题, change the font size Ax.set_xlabel (' x axis ', fontsize=18) #设置x轴, the title of the y-axis ax.set_ Ylabel (' Y axis ', fontsize=18) plt.yticks (fontsize=14) #刻度字体大小plt. Xticks (fontsize=14) plt.legend ([' label '],fontsize=15]  #标签内容字体大小plt. Savefig (' Line chart ', dpi=100)  #保存图片, you can set the DPI

2. Column Chart

data_bar1 = data[' Tip '].groupby (data[' Day '). Mean () #统计数据, grouped by week data_bar2 = data[' Tip '].groupby (data[' time '). Mean () #统计数据, grouped by week error = data[' Tip '].groupby (data[' time '). STD () Fig,ax = Plt.subplots (   #画出两个区域ax) [0].bar (data _bar1.index,data_bar1.values)  #第一个区域怎么画ax [0].set_xlabel (' Tip week Diagram ', fontsize=16) ax[0].set_ylabel (' Tip ', FONTSIZE=16) ax[0].legend ([' Week ']) Ax[0].set_ylim (0,4)  #设置Y轴最大最小值ax [1].bar (data_bar2.index,data_bar2.values) # How to draw a second area ax[1].errorbar (data_bar2.index,data_bar2.values,yerr = Error,ls = ' None ', color= ' #96CDCD ', lw=6)  # Add the Variance chart ax[1].legend ([' Lunch and Dinner ']) ax[1].set_xlabel (' Tip dinner diagram ', fontsize=16) ax[1].set_xticklabels ([' Dinner ', ' Luncheon '])   # Change the name of the axis Ax[1].set_ylim (0,4)  #设置Y轴最大最小值fig. Set_size_inches (12,6)  #设置整个图的大小plt. Savefig (' Column chart ', dpi=100)  #保存图片, you can set the DPI

3, the column chart of the horizontal axis

Data_barh = data[' Tip '].groupby (data[' sex '). Mean ()  #统计数据, male and female tip fig,ax = Plt.subplots () Ax.barh (Data_barh.index, data_barh.values,0.4)  #0.4 is width fig.set_size_inches (6,2) plt.title (' Sideways ') ax.set_ylabel (' sex ', fontsize=20) ax.set _xlabel (' tip ', fontsize =) plt.xticks (fontsize=14) ax.set_yticklabels ([' Male ', ' female '])

  

4. Pie chart

Data_pie = data[' Size '].groupby (data[' size '). Size () Fig,ax = Plt.subplots () ax.pie (data_pir,autopct= '%1.1f%% ', Labels=data_pie.index,colors = [' #B0E0E6 ', ' #B0C4DE ', ' #A6A6A6 ', ' #FF3E96 ', ' #FFB5C5 ', ' #FFEBCD ']) #数据源, display the value, display the label, Color Fig.set_size_inches (8,8)  #如果两个数字不相等会变成椭圆

 

5. Scatter chart

Fig,ax = Plt.subplots () ax.scatter (data[' tip '],data[' Total_bill ']) fig.set_size_inches (8,6) ax.set_xlabel (' Tip ', fontsize=18) Ax.set_ylabel (' Total consumption ', fontsize=18) plt.xticks (fontsize=12) plt.yticks (fontsize=12)

  

6, several areas of the drawing method (one is using the above column chart that way Fig,ax = Plt.subplots), the other is the following, this can be customized to occupy the number of spaces)

Fig = plt.figure () Ax1 = Plt.subplot2grid ((2,3), (0,0)) Ax1.bar (data_bar.index,data_bar.values) fig.set_size_inches ( 12,6) Ax2 = Plt.subplot2grid ((2,3), (0,1), colspan=2) #占据几个空额, can also be rowspan, one is horizontal, one is vertical ax2.scatter (data[' Tip '],data[' Total_bill ']) ax3 = Plt.subplot2grid ((2,3), (1,0)) Ax3.barh (data_barh.index,data_barh.values)

7, two column chart comparison

Fig,ax = Plt.subplots () Ax.bar (Np.arange (4), data_bar.values,0.3)    #横坐标先用数字代替ax. Bar (Np.arange (4) +0.3,data_ bar.values*2,0.3)   #偏移一定量ax. Set_xticks (Np.arange (4) +0.15)   #重新设置x轴的位置ax. Set_xticklabels (Data_bar.index)   #重新设置名称

  

Iv. Drawing (Seaborn)

  

 

  

  

  

Data Analysis--graphing (Python)

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.