春江暮客

春江暮客的个人学习分享网站

分类 Technology 中的文章

Detailed Examples of Seaborn Plotting Kernel Density Curves

Detailed Examples of Seaborn Plotting Kernel Density Curves

2019年1月20日 | Technology

In a frequency distribution histogram, when the sample size is sufficiently enlarged to its limit, and the bin width is infinitely shortened, the step-like broken line in the frequency histogram will evolve into a smooth curve. This curve is called the density distribution curve of the population.

This article walks through how to use Seaborn with the Iris dataset in Pandas to plot several common kinds of kernel density curves.


1. Basic Density Curve

    import seaborn as sns
    import pandas as pd
    sns.set(color_codes=True)
    sns.set_style("white")
    df = pd.read_csv('iris.csv')
    sns.kdeplot(df['sepal_width'])

‘Detailed Examples of Seaborn Plotting Kernel Density Curves’

To plot a kernel density curve using Seaborn, you only need to use kdeplot. Note that a density curve only requires one variable; here we choose the sepal_width column.


2. Density Curve with Shading

    import seaborn as sns
    import pandas as pd
    sns.set(color_codes=True)
    sns.set_style("white")
    df = pd.read_csv('iris.csv')
    sns.kdeplot(df['sepal_width'], fill=True)

‘Detailed Examples of Seaborn Plotting Kernel Density Curves’

……

阅读全文

Drawing a Stunning "Dream of the Red Chamber" Word Cloud with Python 3

Drawing a Stunning "Dream of the Red Chamber" Word Cloud with Python 3

2019年1月17日 | Technology

Word clouds, which I’m sure you’ve all seen, are created using wordcloud, a famous Python library. This article will detail how to use wordcloud to create a word cloud for “Dream of the Red Chamber,” one of China’s Four Great Classical Novels.


1. Preparation

This involves three parts:

1. Install wordcloud and jieba

You can install them with pip install wordcloud and pip install jieba.

2. Prepare a Chinese font file

3. Prepare the text file

The .txt file and the font file are bundled so this example is easier to reproduce.


2. Drawing the “Dream of the Red Chamber” Word Cloud

Here’s the code directly:

    from wordcloud import WordCloud
    import jieba
    import matplotlib.pyplot as plt
    text = "".join(jieba.cut(open("红楼梦.txt").read()))
    wordcloud = WordCloud(font_path="kaibold.ttf").generate(text)

    # Display the generated image:
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.margins(x=0, y=0)
    plt.show()

《Drawing a Stunning “Dream of the Red Chamber” Word Cloud with Python 3》

……

阅读全文

友情链接

其它