大家好,我是就读于西交利物浦商分专业的Dragee。
因为是零基础学习数据分析,作为小白。Codecademy持续10周的Introduction to Data Analysis课程非常适合入门。还提供Python代码的学习,因此我选择了这个课程。
Introduction to Data Analysis | CodecademyIntroduction to Data Analysiswww.codecademy.com
课程主要分为五个部分,除此之外还有一个Project,售价是199美元。
对于我这样的初学者还是很实用的。另外,Evan的这篇文章给提供了很多全面的资料和网站作为参考。Evan:零基础学习Python数据分析zhuanlan.zhihu.com
Unit 1 用Matplotlib做数据可视化的Basic Command
一、使用Pyplot模块画图
参考资料:matplotlib-user-guide-zh/3.1.md at master · wizardforcel/matplotlib-user-guide-zh · GitHubwizardforcel/matplotlib-user-guide-zhgithub.com
首先要把pyplot功能导入进来
from matplotlib import pyplot as plt
用plt.plot()画图
用plt.show()语句可以显示出点图,如:
from matplotlib import pyplot as plt
days = range(7)
money_spent = [10, 12, 12, 10, 14, 22, 24]
plt.plot(days, money_spent)
plt.show()图1
二、使用Python语句设置图表
在同一组轴上显示多个线图用于比较多个Datasets时,Matpltlib会自动用不同颜色标记:
from matplotlib import pyplot as plt
time = [0, 1, 2, 3, 4]
revenue = [200, 400, 650, 800, 850]
costs = [150, 500, 550, 550, 560]
plt.plot(time,revenue)
plt.plot(time,costs)
plt.show()图2
当然,我们也可以自己设置颜色(使用HTML color name or HEX code)_Key word:color ;设置是否虚线(dotted or dashed)_Key word:linestyle;设置标记(marker)_Key word:maker。例如:
from matplotlib import pyplot as plt
time = [0, 1, 2, 3, 4]
revenue = [200, 400, 650, 800, 850]
costs = [150, 500, 550, 550, 560]
plt.plot(time,revenue,color='purple',linestyle='--')
#Dashed:linestyle='--' Dotted:linestyle=':' No line:linestyle=''
plt.plot(time,costs,color='#82edc9',marker='s')
#circle:marker='o' square:marker='s' star:marker='*'
plt.show()图3
就会出现上图的效果了。
有时候,放大或缩小是很有帮助的,特别是如果有一些细节需要处理的话。放大,我们可以用plt.axis。例如:上面的图3,我想查看下x从0到4,y从300到400的具体情况,结果如下图
plt.axis([0,4,300,400]
plt.show()
如果我们要展示图表时,就需要给我们的图表加上标题,给x轴和Y轴加上Label,这时要使用的语句分别是:plt.title和plt.xlabel&plt.ylabel。所有这些命令都需要一个字符串String,它是单引号或双引号中的一组字符。例如:
"This is a string"
'This is also a string'
'This is NOT a string (the quotes do not match)"
上例中,如果我们想给x添加标签“Time”,给图表添加标题“Revenue VS Cost”:
plt.xlabel("Time")
plt.title("Revenue VS Cost")
那么,在图中有多条线的时候,如何标记不同的线,就需要用到plt.legend.例如:
from matplotlib import pyplot as plt
months = range(12)
hyrule = [63, 65, 68, 70, 72, 72, 73, 74, 71, 70, 68, 64]
kakariko = [52, 52, 53, 68, 73, 74, 74, 76, 71, 62, 58, 54]
gerudo = [98, 99, 99, 100, 99, 100, 98, 101, 101, 97, 98, 99]
legend_labels=['Hyrule','Kakariko','Gerudo Valley']
plt.plot(months, hyrule)
plt.plot(months, kakariko)
plt.plot(months, gerudo)
#create your legend here
plt.legend(legend_labels,loc=9)
plt.show()
三、多图展示
有时候需要将一些相同值域的图表并排显示(如图所示):
这时我们可以使用plt.subpot命令,语句中需包含3项,分别是
The number of rows of subplots,The number of columns of subplots,The index of the subplot we want to create
例如,plt.subplot(2,3,4)指的是上图的Subplot4。创建每个图表后我们都可以以此语句指定其位置。我们甚至可以设置图与图之间的间隙。此时要用到的语句是plt.subplot_adjust。此命令有一些关键参数,分别是:left — the left-side margin, with a default of 0.125. You can increase this number to make room for a y-axis label(左侧边距,默认值为0.125。可以增加这个边距为y轴标签腾出空间)
right — the right-side margin, with a default of 0.9. You can increase this to make more room for the figure, or decrease it to make room for a legend(右侧边距,默认值为0.9)
bottom — the bottom margin, with a default of 0.1. You can increase this to make room for tick mark labels or an x-axis label(底部边距,默认值为0.1。您可以增加这一边距以腾出空间的刻度标记或X轴标签)
top — the top margin, with a default of 0.9(顶部边距,默认值为0.9)
wspace — the horizontal space between adjacent subplots, with a default of 0.2(横向间距,默认值0.2)
hspace — the vertical space between adjacent subplots, with a default of 0.2(纵向间距,默认值0.2)
例如☆:
from matplotlib import pyplot as plt
x = range(7)
straight_line = [0, 1, 2, 3, 4, 5, 6]
parabola = [0, 1, 4, 9, 16, 25, 36]
cubic = [0, 1, 8, 27, 64, 125, 216]
# Subplot 1
plt.subplot(2, 1, 1)
plt.plot(x, straight_line)
# Subplot 2
plt.subplot(2, 2, 3)
plt.plot(x, parabola)
# Subplot 3
plt.subplot(2, 2, 4)
plt.plot(x, cubic)
plt.subplots_adjust(wspace=0.35, bottom=0.2)
plt.show()
四、标记
如果我们要标记其中的某一个subplot的话,就要用到轴对象axis object的概念,它让我们修改属于一个特定的subplot的axis。只有一个图的情况也是如此,如:
ax = plt.subplot()
plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16])
plt.plot([0, 1, 2, 3, 4], [0, 1, 8, 27, 64])
ax.set_xticks([1, 2, 4])
from matplotlib import pyplot as plt
month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct", "Nov", "Dec"]
months = range(12)
conversion = [0.05, 0.08, 0.18, 0.28, 0.4, 0.66, 0.74, 0.78, 0.8, 0.81, 0.85, 0.85]
plt.xlabel("Months")
plt.ylabel("Conversion")
plt.plot(months, conversion)
ax = plt.subplot()
ax.set_xticks(months)
ax.set_xticklabels(month_names)
ax.set_yticks([0.10, 0.25, 0.5, 0.75])
ax.set_yticklabels(["10%", "25%", "50%", "75%"])
plt.show()
五、Fill Between 功能
from matplotlib import pyplot as plt
hours_reported =[3, 2.5, 2.75, 2.5, 2.75, 3.0, 3.5, 3.25, 3.25, 3.5, 3.5, 3.75, 3.75,4, 4.0, 3.75, 4.0, 4.25, 4.25, 4.5, 4.5, 5.0, 5.25, 5, 5.25, 5.5, 5.5, 5.75, 5.25, 4.75]
exam_scores = [52.53, 59.05, 61.15, 61.72, 62.58, 62.98, 64.99, 67.63, 68.52, 70.29, 71.33, 72.15, 72.67, 73.85, 74.44, 75.62, 76.81, 77.82, 78.16, 78.94, 79.08, 80.31, 80.77, 81.37, 85.13, 85.38, 89.34, 90.75, 97.24, 98.31]
plt.figure(figsize=(10,8))
# Create your hours_lower_bound and hours_upper_bound lists here
hours_lower_bound = [element - (element * 0.2) for element in hours_reported]
hours_upper_bound = [element + (element * 0.2) for element in hours_reported]
# Make your graph here
plt.plot(exam_scores, hours_reported, linewidth=2)
plt.fill_between(exam_scores, hours_lower_bound, hours_upper_bound, alpha = 0.2)
plt.xlabel("Score")
plt.title("Time spent studying vs final exam scores")
plt.ylabel('Hours studying (self-reported)')
plt.show()
plt.savefig("my_line_graph.png")
六、清除和保存
清除:
plt.close('all')
保存:我们可以使用命令plt.savefig储存成许多不同的文件格式,如PNG,SVG,或PDF。
plt.savefig('name_of_graph.png')
设置图片大小:
plt.figure(figsize=(width,height))
例如:
from matplotlib import pyplot as plt
word_length = [8, 11, 12, 11, 13, 12, 9, 9, 7, 9]
power_generated = [753.9, 768.8, 780.1, 763.7, 788.5, 782, 787.2, 806.4, 806.2, 798.9]
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]
plt.close('all')
plt.plot(years,word_length)
plt.savefig('winning_word_lengths.png')
plt.figure(figsize=(7,3))
plt.plot(years,power_generated)
plt.savefig('power_generated.png')
python是个很好的数据分析工具,不需要学习很多编程语言也可以操作。
因为自己也是在学习中,难免会有一些错误,欢迎讨论指正
希望大家和我一起来交流学习