数据科学导论——数据可视化进阶

第1关:热图

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
def student():

    # ********* Begin *********#
    df=pd.read_csv("Task1/listings.csv")
    plt.figure(figsize=(10,10))
    sns.heatmap(df[['price','minimum_nights','availability_365','reviews_per_month','number_of_reviews']].corr(),annot=True)
    plt.xticks(rotation=90)
    plt.savefig("Task1/img/T1.png")
    plt.show()

    # ********* End *********#

第2关:文本可视化

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
import seaborn as sns
from PIL import Image
import requests
import numpy as np
import pandas as pd
def student():

    # ********* Begin *********#
    # 补全所有None的部分
    df=pd.read_csv("Task2/listings.csv")
    mask = np.array(Image.open(requests.get('http://www.clker.com/cliparts/O/i/x/Y/q/P/yellow-house-hi.png', stream=True).raw))
    filtered_words = {"Amsterdam", "apartment", "room", "bedroom", "studio", "city"}
    wordcloud = WordCloud(
                        background_color = "white",
                        stopwords = set(STOPWORDS).union(filtered_words),
                        max_words = 100,
                        max_font_size = 40,
                        mask = mask,
                        random_state=2019
                        ).generate(str(df["name"]))
    plt.figure(figsize = (10,10))
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.savefig("Task2/img/T1.png")
    plt.show()
    # ********* End *********#

第3关:作图调整和美化

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
def student():

    # ********* Begin *********#
    plt.figure(figsize = (10,10))
    df=pd.read_csv("Task3/listings.csv")
    d=df["price"].groupby(df["room_type"]).mean()
    sns.set(style = 'whitegrid')
    sns.barplot(y=d.values,x=d.index)
    plt.xticks(rotation=90)
    plt.savefig("Task3/img/T1.png")
    plt.show()


    # ********* End *********#

你可能感兴趣的:(数据挖掘)