投掷一个六面骰子,绘制出投掷100次各点数出现的分布情况
from die import Die
import pygal
die = Die()
results = []
for roll_num in range(100):
result = die.roll()
results.append((result))
fresequences = []
for value in range(1,die.num_sides+1):
fresequence = results.count(value)
fresequences.append(fresequence)
#对结果进行可视化
#为创建条形图,我们创建了一个pygal.Bar()实例
hist = pygal.Bar()
hist.title = "Results of rolling one D6 100times."
hist.x_labels = range(1,7)
hist.x_title = "Result"
hist._y_title = "fresequence of results"
hist.add('D6',fresequences)
#将这个 图表渲染为一个SVG文件,这种文件的扩展名必须为.svg。
#要查看生成的直方图,简单的方式是使用Web浏览器。为此,在任何Web浏览器中新建一 个标签页,再在其中打开文件die_visual.svg
hist.render_to_file('die_visual.svg')
投掷2个六面骰子,绘制出投掷1000次各点数出现的分布情况
绘制气温表
import csv
import matplotlib.pyplot as plt
from datetime import datetime
filename = 'sitka_weather_07-2014.csv'
with open(filename)as f:
#.reader创建一个阅读器对象
reader = csv.reader(f)
header_row = next(reader)
#存储最高温度
hights = []
datetimes = []
lows = []
for row in reader:
hight = int(row[1])
#模块datetime中的datetime类,然后调用方法strptime(),
# 并将包含所需日 期的字符串作为第一个实参。第二个实参告诉Python如何设置日期的格式。
current_date = datetime.strptime(row[0], "%Y-%m-%d")
low = int(row[3])
hights.append(hight)
datetimes.append(current_date)
lows.append(low)
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(datetimes,hights,c="red")
plt.plot(datetimes,lows,c = "blue")
#在最高温度最低温度之间填充颜色
#实参alpha指定颜色的透明度。Alpha值为0表示完全透明,1(默认设置)表示完全不透明
plt.fill_between(datetimes,hights,lows,facecolor = 'blue',alpha = 0.1)
plt.title("Daily high temperatures, July 2014", fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()