这篇文章会延伸昨天所学
改变参数的使用
并且画出更多的图表
文章内容分别是
3. 折线图、散布图跟柱状图
4. 长条图
5. 绘制在子图上
昨天画过的图今天来更改参数
将图表的展示方式做一点调整
figsize
调整图片大小,style
改变线条的样式,color
改变线条的颜色,legend
指定图例
group_b.plot(y=["math score","reading score"],figsize=(20,5), style="--", color=["purple","pink"], legend=["math score","reading score"])
接下来将同样的比较数据放到散布图看看每个人成绩分布的情形
plt.scatter(group_b.index ,group_b["math score"],color="purple")
plt.scatter(group_b.index, group_b["reading score"],color="pink")
若是现在想要依照成绩高低看分布的情行
可以使用柱状图看看成绩的分布状况
利用alpha
调整透明度,color
调整颜色,让叠加在一起的图更好比较
plt.title()
设置标题
plt.xlabel()
, plt.ylabel.()
分别设置x,y轴名称
plt.legend()
设置图例
plt.hist(group_b["math score"],color="g",alpha=0.3)
plt.hist(group_b["reading score"],alpha=0.4)
plt.hist(group_b["writing score"],alpha=0.5,color="pink")
plt.title('score distribution')
plt.xlabel("score")
plt.ylabel("numbers")
现在如果想看各组在各科的成绩平均数比较
可以使用长条图表示
首先也是利用.groupby()
再接着使用.mean()
算出平均数
race_ethnicity = df.groupby("race/ethnicity").mean()
race_ethnicity
下面介绍两种绘图的方式
直接利用整理过後的dataframe接上.plot.bar()
.plot()
之後才在括号里面调整参数
race_ethnicity.plot(kind='bar', #图表类型
title='scores in different group', #标题
xlabel='gruoup', #x轴标题
ylabel='score', #y轴标题
legend=True, # 显示图例
figsize=(10, 5)) # 设定图表大小
现在练习将上面画过的图
利用子图的排列
放在同一张画布上
先建立四张子图
fig = plt.figure(figsize=(20,10))
axe1 = fig.add_subplot(2, 2, 1)
axe2 = fig.add_subplot(2, 2, 2)
axe3 = fig.add_subplot(2, 2, 3)
axe4 = fig.add_subplot(2, 2, 4)
分别将四张图表放上去
子图若要加上标题有两种方式
ax.title.set_text(" ")
ax.set_title(" ")
#子图一放上性别占整体的比例
axe1.pie(numbers_of_gender, labels=type_of_gender,autopct="%0.2f%%")
axe1.title.set_text("portion of gender")
#子图二放上各组别占整体的比例
axe2.pie(amounts, labels=category,autopct="%0.2f%%")
axe2.title.set_text("portion of groups")
#子图三放上各组在各科的成绩平均数比较
axe3.set_title("scores on different group")
race_ethnicity.plot.bar(ax=axe3)
#子图四看整体资料依照成绩高低的分布
axe4.hist(df["math score"],color="g",alpha=0.3) #记得调整透明度
axe4.hist(df["reading score"],alpha=0.4)
axe4.hist(df["writing score"],alpha=0.6,color="pink")
score_labels=["math score","reading score","writing score"]
axe4.legend(labels=score_labels)
axe4.set_title("scores distribution of all")
这几篇文章大致上介绍了matplotlib这个套件的操作
加上实际资料的操作
已经对将资料利用图表视觉化有了一定程度的了解
但是很多时候还是要依据使用的情形上网查有没有更好的表示方法
像是在画histogram的时候,如果按照原本的设定叠加图形的话
会很明显看不出任何线索
但是如果将透明度慢慢调整,就可以看出不同科目的成绩分布状况
>>: [2021铁人赛 Day25] Web Exploitation Web渗透题目 01
(三)资讯安全风险管理组织架构 在执行资讯安全风险管理之前,相关组织、角色及权责应预先设置,相关授权...
去年宝哥在 Modern WEB 中介绍过 Angular 有时会发生版本不相容的 issue , ...
用工具完成任务 ≠ 了解工具。 随着时代演进,大部分的公司都采用框架来加速开发效率;自从有了框架,...
当年求学的时候有个老师很喜欢说 "老师说这个很重要的意思就是......这个很~ 重 ~ ...
Cloud Run 是这次要介绍的最後一种部属服务方式。它是一种基於 Container 的 Ser...