Xcu -- data visualization -- Experiment 2

Posted by shergar1983 on Fri, 12 Jun 2020 08:44:49 +0200

Data visualization experiment 2

preface

Through pandas in python, the experiment is completed with bar graph, bar graph and line graph.

Title Requirements

1. "Tourism annual data. xls" and "tourism statistical data. csv" are tourism statistical data from 2009 to 2018, including the number of domestic tourists, rate income, per capita consumption and other data. Please show the data according to the following requirements and analyze the trend of relevant data over time from the figure. (data visualization tools can be python, R, AI)

(1) Use line chart to draw the total cost data of travel agency and domestic tourism.

(2) Use the histogram to draw the per capita consumption of domestic tourism, urban and rural tourism.

(3) The number of inbound tourists, foreign tourists, Hong Kong and Macao compatriots and Taiwan compatriots' inbound tourists are drawn with bar chart.

(4) Draw the data of domestic tourists, urban and rural tourists with bar chart.

Source data

  • Annual tourism data
Database: annual data
Time: last 10 years
index 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009
Number of travel agencies 27939 27621 26650 26054 24944 23690 22784 20399
Total number of star hotels 11685 12327 12803 13293 12807 13513 13991 14237
Inbound tourists (10000 person times) 14120 13948.24 13844.38 13382.04 12849.83 12907.78 13240.53 13542.35 13376.22 12647.59
Foreign inbound tourists (10000 person times) 2916.53 2815.12 2598.54 2636.08 2629.03 2719.16 2711.2 2612.69 2193.75
Inbound tourists from Hong Kong and Macao compatriots (10000 person times) 10444.59 10456.26 10233.64 9677.16 9762.5 9987.35 10304.85 10249.48 10005.44
Inbound tourists of Taiwan compatriots (10000 person times) 587.13 573 549.86 536.59 516.25 534.02 526.3 514.06 448.4
Inbound overnight tourists (10000 person times) 6073.84 5926.73 5688.57 5562.2 5568.59 5772.49 5758.07 5566.45 5087.52
Number of domestic residents leaving the country (10000 person times) 16199 14272.74 13513 12786 11659.32 9818.52 8318.17 7025 5738.65 4765.62
Number of domestic residents leaving the country for private reasons (10000 person times) 13581.56 12850 12172 11002.91 9197.08 7705.51 6411.79 5150.79 4220.97
Domestic tourists (10000 person times) 554000 500100 444000 400000 361100 326200 295700 264100 210300 190200
Foreign exchange income from international tourism (USD million) 127100 123417 120000 113650 56913 51664 50028 48464 45814 39675
Total cost of domestic tourism (100 million yuan) 51278 45660.77 39390 34195.1 30311.86 26276.1 22706.2 19305.39 12579.77 10183.7
Note: number of border people entering China in 2016
Data source: National Bureau of Statistics
  • Tourism statistics
Database: annual data
Time: last 10 years
index 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009
Domestic tourists (million) 5540 5001 4440 4000 3611 3262 2957 2641 2103 1902
Domestic tourists of urban residents (million person times) 3677 3195 2802 2483 2186 1933 1687 1065 903
Domestic tourists of rural residents (million person times) 1324 1240 1188 1128 1076 1024 954 1038 999
Total cost of domestic tourism (100 million yuan) 51278 45660.8 39390 34195.1 30311.9 26276.1 22706.2 19305.4 12579.8 10183.7
Total domestic tourism expenditure of urban residents (100 million yuan) 37673 32241.3 27610.9 24219.8 20692.6 17678 14808.6 9403.8 7233.8
Total domestic tourism expenditure of rural residents (100 million yuan) 7987.7 7147.8 6584.2 6092.1 5583.5 5028.2 4496.8 3176 2949.9
Per capita cost of domestic tourism (yuan) 913 888.2 857 839.7 805.5 767.9 731 598.2 535.4
Per capita cost of domestic tourism for urban residents (yuan) 1024.6 1009.1 985.5 975.4 946.6 914.5 877.8 883 801.1
Per capita cost of domestic tourism for rural residents (yuan) 603.3 576.4 554.2 540.2 518.9 491 471.4 306 295.3
Data source: National Bureau of Statistics

Coding implementation

import pandas as pd
import matplotlib.pyplot as plt
import os

# Data root
data_path = r'data/Experiment 2'

def dataSet(filename, header=2, nrows=12, index_col=0, usercols=None, file_type="excel", encoding="utf-8"):
    if file_type == "excel":
        data = pd.read_excel(os.path.join(data_path, filename), header=header, nrows=nrows, index_col=index_col,
                             usecols=usercols, encoding=encoding)
    elif file_type == "csv":
        data = pd.read_csv(os.path.join(data_path, filename), header=header, nrows=nrows, index_col=index_col,
                           usecols=usercols, encoding=encoding)
    else:
        print("Error ")
        return None

    return data
def showBarh(data, im_name, im_title,xlabel="xlabel", ylabel="ylabel"):
    ax = data.plot(kind='barh', title=im_title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    plt.show()
    # preservation
    fig = ax.get_figure()
    fig.savefig(os.path.join(data_path, im_name + ".jpg"))

def showBar(data, im_name, im_title,xlabel="xlabel", ylabel="ylabel"):

    ax = data.plot(kind='bar', title=im_title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    plt.show()
    # preservation
    fig = ax.get_figure()
    fig.savefig(os.path.join(data_path, im_name + ".jpg"))


def showCumsum(data, im_name, im_title, xlabel="xlabel", ylabel="ylabel"):
    ax = data.plot(title=im_title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    plt.show()
    # preservation
    fig = ax.get_figure()
    fig.savefig(os.path.join(data_path, im_name + ".jpg"))



if __name__ == "__main__":
    """
    //Read data
    """
    data1 = dataSet(filename="Annual tourism data.xls")
    data2 = dataSet(filename="Tourism statistics.csv", header=2, nrows=9, file_type="csv", encoding="GBK")
    data2 = data2.T
    data1 = data1.T

    # print(data1 ["total domestic tourism cost (100 million yuan)", "number of travel agencies]]])
    """
    //Experiment 2-1-1: draw the total cost data of travel agencies and domestic tourism with line chart
    """
    showCumsum(
        data=data1[["Total cost of domestic tourism(100 million yuan)", "Number of travel agencies(individual)"]],
        im_name="Experiment 2-1-1",
        im_title="Total travel agency and domestic travel cost data (line chart)",
        xlabel="particular year",
        ylabel="numerical value"
    )
    """
    //Experiment 2-1-2: draw the per capita consumption of domestic tourism, urban and rural tourism with histogram.
    """
    showBar(
        data2[["Per capita cost of domestic tourism(element)","Per capita cost of domestic tourism for urban residents(element)","Per capita cost of domestic tourism for rural residents(element)"]],
        im_name="Experiment 2-1-2",
        im_title="Per capita consumption (bar chart)",
        xlabel="particular year",
        ylabel="numerical value"
    )

    """
    //Experiment 2-1-3: draw the number of inbound tourists, foreigners, Hong Kong and Macao compatriots, and Taiwan compatriots.
    """
    showBar(
        data1[["Inbound tourists(10000 person times)","Foreign tourists(10000 person times)","Inbound tourists from Hong Kong and Macao compatriots(10000 person times)","Inbound tourists from Taiwan compatriots(10000 person times)"]],
        im_name="Experiment 2-1-3",
        im_title="Number of inbound tourists (bar chart)",
        xlabel="particular year",
        ylabel="numerical value"
    )
    """
    //Experiment 2-1-4: draw the data of domestic tourists, urban and rural tourists with bar graph.
    """
    showBarh(
        data2[["Domestic tourists(Million people)","Urban residents domestic tourists(Million people)","Rural residents domestic tourists(Million people)"]],
        im_name="Experiment 2-1-4",
        im_title="Number of inbound tourists (bar chart)",
        xlabel="particular year",
        ylabel="numerical value"
    )

    plt.show()

Topics: encoding Python Database Excel