python学习的第三天

matplotlib

导入

from matplotlibimport pyplotas plt

from wordcloudimport WordCloud

在显示Plots界面中文

plt.rcParams["font.sans-serif"] = ['SimHei']

plt.rcParams['axes.unicode_minus'] =False

import jieba

import numpyas np

使用100个点 绘制 [0 , 2π]正弦曲线图

.linspace 左闭右闭区间的等差数列

x = np.linspace(0, 2*np.pi, num=100)

y = np.arctan(x)

正弦和余弦在同一坐标系下

cosy = np.cos(x)

plt.plot(x, y, color='g', linestyle='--',label='sin(x)')

plt.plot(x, cosy, color='r',label='cos(x)')

plt.xlabel('时间(s)')

plt.ylabel('电压(V)')

plt.title('欢迎来到python世界')

图例显示

plt.legend()

plt.show()

柱状图

import string

from randomimport randint

x = ['物品{}'.format(x)for xin string.ascii_uppercase[:5]]

y = [randint(200,500)for _in range(5)]

plt.xlabel('品牌')

plt.ylabel('价格')

plt.title('欢迎来到python世界')

图例

plt.bar(x, y)

plt.show()

饼图

from randomimport randint

import string

counts = [randint(3500, 9000)for _in range(6)]

labels = ['员工{}'.format(x)for xin string.ascii_lowercase[:6] ]

距离圆心点距离

explode = [0.1,0,0, 0, 0,0]

colors = ['red', 'purple','blue', 'yellow','gray','green']

plt.pie(counts,explode = explode,shadow=True, labels=labels, autopct ='%1.1f%%',colors=colors)

plt.legend(loc=2)

plt.axis('equal')

plt.show()

散点图

均值为 0 标准差为1 的正太分布数据

x = np.random.normal(0, 1, 100)

y = np.random.normal(0, 1, 100)

plt.scatter(x, y)

plt.show()

x = np.random.normal(0, 1, 1000000)

y = np.random.normal(0, 1, 1000000)

alpha透明度

plt.scatter(x, y, alpha=0.1)

plt.show()


实验绘制 三国top10 饼图

1.读取小说内容

with open('./路径/文件名.txt', 'r', encoding='utf-8')as f:

words = f.read()

counts = {}

    excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",

                "如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",

                "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",

                "孔明曰","玄德曰","刘备","云长"}

2. 分词

    words_list = jieba.lcut(words)

    for wordin words_list:

        if len(word) <=1:

            continue

        else:

3.更新字典中的值

            # counts[word] = 取出字典中原来键对应的值+ 1

            # counts[word] = counts[word] + 1  # counts[word]如果没有就要报错

            # 字典。get(k) 如果字典中没有这个键 返回NONE

            counts[word] = counts.get(word, 0) +1

4.词语过滤,删除无关词,重复词

    counts['孔明'] =  counts['孔明'] +  counts['孔明曰']

    counts['玄德'] = counts['玄德'] + counts['玄德曰'] +counts['刘备']

    counts['关公'] = counts['关公'] +counts['云长']

    for wordin excludes:

    del counts[word]

5.排序[(), ()]

    items =list(counts.items())

    def sort_by_count(x):

        return x[1]

    items.sort(key=sort_by_count, reverse=True)

    counts = []

    labels = []

    for iin range(10):

        role, count = items[i]

        counts.append(count)

        labels.append(role)

饼图

距离圆心点距离

explode = [0,0,0, 0, 0,0,0, 0, 0,0]

colors = ['red', 'purple','blue', 'yellow','gray','green','purple','orange','brown','pink']

plt.pie(counts,explode = explode,shadow=True, labels=labels, autopct ='%1.1f%%',colors=colors)

plt.legend(loc=2)

plt.axis('equal')

plt.show()

你可能感兴趣的:(python学习的第三天)