1.柱状图
from matplotlib import pyplot as plt
from matplotlib import font_manager
a = ['流浪地球','疯狂的外星人','飞驰人生','大黄蜂','熊出没·原始时代','新喜剧之王']
b = [38.13,19.85,14.89,11.36,6.47,5.93]
my_font = font_manager.FontProperties(fname='/data/course_data/data_analysis/STSONG.TTF')
plt.figure(figsize=(20,8),dpi=80)
# 绘制柱状图,plt.bar(x,height,width,color),
#x:记录x轴上的标签
#height:记录每个柱形的高度
#width:设置柱形的宽度
#color:设置柱形的颜色,传入颜色值的列表,例如['blue','green','red']
rects = plt.bar(a,b,width=0.3,color=['red','green','blue','cyan','yellow','gray'])
#自定义X轴和Y轴得刻度
plt.xticks(a,fontproperties=my_font)
plt.yticks(range(0,41,5),range(0,41,5))
# 在条形图上加标注(水平居中)
for rect in rects:
height = rect.get_height()
#plt.text(x,y,s,ha,va)为每个柱形标注高度数值,x和y坐标,参数s记录标注的内容,参数ha和va分别用于设置水平和垂直方向的对齐方式
#rect.get_height()获取柱体高度,rect.get_x()获取柱体左侧X值,rect.get_width(),获取柱体宽度
plt.text(rect.get_x() + rect.get_width() / 2, height+0.3, str(height),ha="center")
plt.show()
2.直方图
from matplotlib import pyplot as plt
from matplotlib import font_manager
time = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114,
119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99,
136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144,
105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123,
117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116,
108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125,
138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,
107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109,
106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123,
111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109,
141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103,
130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98,
117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111,
101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116,
111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
my_font = font_manager.FontProperties(fname='/data/course_data/data_analysis/STSONG.TTF',size=10)
# 2)创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 3)绘制直方图
# 设置组距
distance = 2
# 计算组数
group_num = int((max(time) - min(time)) / distance)
# 绘制直方图plt.hist(data, bins, facecolor, edgecolor),
#data:绘图用到的数据
#height:直方图的条形数目
#bins:设置柱形的宽度
#facecolor:矩形的填充颜色
#edgecolor:条形的边框颜色
plt.hist(time, bins=group_num)
# 修改x轴刻度显示
plt.xticks(range(min(time), max(time))[::2])
# 添加网格显示
plt.grid(linestyle="--", alpha=0.5)
# 添加x, y轴描述信息
plt.xlabel("电影时长大小",fontproperties=my_font)
plt.ylabel("电影的数据量",fontproperties=my_font)
plt.show()
3.散点图
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('/data/course_data/data_analysis/height_weight.csv')
# 设置图形大小
plt.figure(figsize=(20,8),dpi=80)
# 使用plt.scatter(x,y,s,c,marker,alpha,linewidths)绘制散点图
#x,y:数组
#s:散点图中点的大小,可选
#c:散点图中点的颜色,可选
#marker:散点图的形状,可选
#alpha:表示透明度,在 0~1 取值,可选
#linewidths:表示线条粗细,可选
plt.scatter(df['height'],df['weight'],alpha=0.5,c='red')
plt.show()
4.饼图
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname='/data/course_data/data_analysis/STSONG.TTF',size=10)
df = pd.read_excel('/data/course_data/data_analysis/lagou.xlsx')
plt.figure(figsize = (10,8))
num = df.groupby('学历').size()
# 用plt.pie(x, labels, autopct, shadow, startangle)绘制饼图
x:绘制用到的数据
#Tlabels:用于设置饼图中每一个扇形外侧的显示说明文字
#autopct:设置饼图内百分比数据,可以使用format字符串或者format #function,例如’%.1f%%’指小数点后保留1位小数
#shadow:表示是否在饼图下面画阴影,默认值:False,即不画阴影
#Tstartangle:设置起始绘制角度,默认图是从x轴正方向逆时针画起,如果设定startangle=90,则从y轴正方向画起
patches, l_text, p_text = plt.pie(num,labels = num.index, autopct='%.1f%%',shadow=False,startangle=90)
#plt.pie()会有三个返回值
#patches: matplotlib.patches.Wedge列表(扇形实例)
#l_text:label matplotlib.text.Text列表(标签实例)
#p_text:label matplotlib.text.Text列表(百分比标签实例)
for t in l_text:
t.set_fontproperties(my_font)
plt.show()
5.总结