1. 用点加线的方式画出 x=(0,10) 间 sin 的图像
import matplotlib.pyplot as plt #导入matplotlib库简写为plt
import numpy as np
x = np.linspace(0,10) #定义x变量的范围(0,10)
y=np.sin(x)
plt.plot(x,y, 'o-')#用点加线的方式画出x=(0,10)间sin的图像
plt.show()
2. 利用以下数据分别制作水平和垂直柱状图
x = [1,2,3,4,5,6,7,8]
y = [3,1,4,5,8,9,7,2]
label=['A','B','C','D','E','F','G','H']
import matplotlib.pyplot as plt #导入matplotlib库简写为plt
x = [1,2,3,4,5,6,7,8]
y = [3,1,4,5,8,9,7,2]
label=['A','B','C','D','E','F','G','H']
plt.barh(x,y,tick_label = label)#水平柱状图:pkt.barh,tick_label:刻度标签
plt.show()
plt.bar(x,y,tick_label = label)#柱状图:plt.bar,tick_label:刻度标签
plt.show()
3. 自定义图表元素
1) 绘制 x=(0,10)间 sin 的图像,设置线性为虚线
2) 设置 y 轴显示范围为(-1.5,1.5)
3) 设置 x,y 轴标签分别为“variable x”,“value y”
4) 设置图表标题“三角函数”
5) 显示网格
6) 绘制平行于 x轴 y=0.8的水平参考线(提示:使用.axhline)
7) 添加注释文字 sin(x)
import matplotlib.pyplot as plt #导入matplotlib库简写为plt
import numpy as np
x=np.linspace(0,10) #定义x的范围为0~10
y=np.sin(x)
plt.plot(x,y,'--') #绘制图像 其中'--'表示线性为虚线
plt.ylim(-1.5,1.5) #设置 y 轴显示范围为(-1.5,1.5)
plt.xlabel("variable x")#设置x轴标签为“variable x”
plt.ylabel("value y")#设置y轴标签为“value y”
plt.title('三角函数') #设置图表标题“三角函数”
plt.grid() #显示网格
plt.axhline(y=0.8,ls='--',c='r') #ls='--'为设置为虚线、c='r'为设置颜色为红色
#绘制平行于 x轴 y=0.8的水平参考线(提示:使用.axhline)
plt.text(3.2, 0, 'y=sin(x)',weight='bold', color='r')#添加注释文字 sin(x)
# 3.2, 0为坐标位置,'y=sin(x)'为文本,weight='bold'为加粗,color='r'为设置颜色为红色
plt.show()
4. 多子图
1) 在一个 10×10 的画布中,制作 2 个子图,分别显示 sin(x) 和 cos(x)的图像
2) 在 2 个子图中,显示 sin(x)和 cos(x)的图像
3) 设置相同行和列共享 x,y 轴
5. 统计下文中每个单词出现的次数,并利用饼图其中出现次数最多的前五个单词。
Hooray! It's snowing! It's time to make a snowman. James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons. In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels. A moment later, the snowman takes James's hand and goes out. They go up, up, up into the air! They are flying! What a wonderful night! The next morning, James jumps out of bed. He runs to the door. He wants to thank the snowman. But he's gone.
import matplotlib.pyplot as plt
import numpy as np
text = '''Hooray! It's snowing! It's time to make a snowman. \
James runs out. He makes a big pile of snow. He puts a big snowball on top. \
He adds a scarf and a hat. He adds an orange for the nose. \
He adds coal for the eyes and buttons. In the evening, James opens the door. \
What does he see ? The snowman is moving! James invites him in. \
The snowman has never been inside a house. He says hello to the cat. \
He plays with paper towels. A moment later, the snowman takes James's hand and goes out. \
They go up, up, up into the air! They are flying! What a wonderful night! \
The next morning, James jumps out of bed. He runs to the door. \
He wants to thank the snowman. But he's gone.'''
text = text.replace(',', '').replace('.', '').replace('!', '').replace('?', '')
text = text.split() # 默认会以空格,回车符,空格符等作为分割条件。
data = {}
words = []
ct = []
setword = set(text) # 创建集合去重
for i in setword:
count = text.count(i)
words.append(i)
ct.append(count)
# print(i, '出现次数:', count)
data = dict(zip(ct, words))
d_order = sorted(data.items(), key=lambda x: x, reverse=True)
print(d_order[0:5])
data_1 = d_order[0:5] # top5
words_top5 = []
ct_top5 = []
for i in range(5):
words_top5.append(data_1[i][1])
ct_top5.append(data_1[i][0])
# print(words_top5)
# print(ct_top5)
#绘制饼图
plt.pie(np.array(ct_top5), labels=words_top5, autopct='%1.1f%%')
plt.title('WordsCount')
plt.show()