Pyhon项目
数据可视化
import matplotlib.pyplot as plt
x_values = range(1, 1001)
y_values = [x**2 for x in x_values]
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, s=10)
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("值", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.axis([0, 1100, 0, 1100000])
plt.savefig('0.png', bbox_inches='tight')
import csv
filename = 'death_valley_2018_simple.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
for index, column_header in enumerate(header_row):
print(index, column_header)
from die import Die
from plotly.graph_objs import Bar, Layout
from plotly import offline
die1 = Die()
die2 = Die(10)
results = []
for roll_num in range(50000):
result = die1.roll() + die2.roll()
results.append(result)
frequencies = []
max_result = die1.num_sides + die2.num_sides
for value in range(2, max_result+1):
frequency = results.count(value)
frequencies.append(frequency)
x_values = list(range(2, max_result+1))
data = [Bar(x=x_values, y=frequencies)]
x_axis_config = {'title': '结果', 'dtick':1}
y_axis_config = {'title': '结果的频率'}
my_layout = Layout(title = '掷一个D6和一个D10 50000次的结果' , xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data': data, 'layout': my_layout}, filename='d6_d10.html')
from die import Die
from plotly.graph_objs import Bar, Layout
from plotly import offline
die = Die()
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
frequencies = []
for value in range(1, die.num_sides+1):
frequency = results.count(value)
frequencies.append(frequency)
x_values = list(range(1, die.num_sides+1))
data = [Bar(x=x_values, y=frequencies)]
x_axis_config = {'title': '结果'}
y_axis_config = {'title': '结果的频率'}
my_layout = Layout(title = '掷一个D6 1000次的结果' , xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data': data, 'layout': my_layout}, filename='d6.html')
import json
import pandas as pd
filename = 'eq_data_30_day_m1.json'
with open(filename) as f:
all_eq_data = json.load(f)
all_eq_dicts = all_eq_data['features']
mags, titles, lons, lats = [], [], [], []
for eq_dict in all_eq_dicts:
mag = eq_dict['properties']['mag']
title = eq_dict['properties']['title']
lon = eq_dict['geometry']['coordinates'][0]
lat = eq_dict['geometry']['coordinates'][1]
mags.append(mag)
titles.append(title)
lons.append(lon)
lats.append(lat)
import plotly.express as px
data = pd.DataFrame(data=zip(lons, lats, titles, mags), columns=['经度' ,'纬度', '位置', '震级'])
data.head()
fig = px.scatter(data, x='经度', y= '纬度', range_x=[-200, 200], range_y=[-90, 90], width=800, height=800, title='全球地震散点图', size='震级', size_max=10, color='震级', hover_name='位置')
fig.write_html('global_earthquakes.html')
fig.show()
import plotly.express as px
fig = px.scatter(x=lons, y=lats, labels={'x': '经度', 'y': '纬度'}, range_x=[-200, 200], range_y=[-90, 90], width=800, height=800, title='全球地震散点图')
fig.write_html('global_earthquakes.html')
fig.savefig('2.png', bbox_inches='tight')
import matplotlib.pyplot as plt
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.style.use('seaborn-darkgrid')
fig, ax = plt.subplots()
ax.plot(input_values, squares, linewidth=3)
ax.set_title("平方数", fontsize=24)
ax.set_xlabel("值", fontsize=14)
ax.set_ylabel("值的平方", fontsize=14)
ax.tick_params(axis='both', labelsize=14)
ax.plot(squares)
plt.show()
import requests
from plotly.graph_objs import Bar
from plotly import offline
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
response_dict = r.json()
repo_dicts = response_dict['items']
repo_links, stars, labels = [], [], []
for repo_dict in repo_dicts:
repo_name = repo_dict['name']
repo_url = repo_dict['html_url']
repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)
stars.append(repo_dict['stargazers_count'])
owner = repo_dict['owner']['login']
description = repo_dict['description']
label = f'{owner}<br />{description}'
labels.append(label)
data = [{'type': 'bar', 'x': repo_links, 'y': stars,'hovertext': labels, 'marker':{'color': 'rgb(60, 100, 150)', 'line': {'width':1.5, 'color': 'rgb(25, 25, 25)'}}, 'opacity': 0.6}]
my_layout = {'title': 'Github上最受欢迎的项目', 'xaxis': {'title': 'Repository', 'titlefont': {'size': 24}, }, 'yaxis': {'title': 'Stars'},'titlefont': {'size': 24}, }
fig = {'data':data, 'layout': my_layout}
offline.plot(fig, filename='python_repos_html')
import requests
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
response_dict = r.json()
print(f"Total respositories: {response_dict['total_count']}")
repo_dicts = response_dict['items']
print(f"Respositories returned: {len(repo_dicts)}")
repo_dict = repo_dicts[0]
print("\nSelected information about first respon:0")
print(f"Name: {repo_dict['name']}")
print(f"Owner: {repo_dict['owner']['login']}")
print(f"Stars: {repo_dict['stargazers_count']}")
print(f"{repo_dict['updated_at']}")
from random import choice
class RandomWalk:
def __init__(self, num_points=5000):
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
while len(self.x_values) < self.num_points:
x_direction = choice([1, -1])
x_distance = choice([0, 1, 2, 3, 4])
x_step = x_direction * x_distance
y_direction = choice([1, -1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
x = self.x_values[-1] + x_step
y = self.y_values[-1] + y_step
self.x_values.append(x)
self.y_values.append(y)
import matplotlib.pyplot as plt
from random_walk import RandomWalk
while True:
rw = RandomWalk()
rw.fill_walk()
plt.style.use('classic')
fig, ax = plt.subplots( dpi=300)
point_numbers = range(rw.num_points)
ax.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolors='none', s=15)
ax.scatter(0, 0, c='blue', edgecolors='none', s=100)
ax.scatter(rw.x_values[-1], rw.y_values[-1], c='blue', edgecolor='none', s=100)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
keep_running = input('Make another walk? (y/n): ')
if keep_running == 'n':
break
import csv
from datetime import datetime
import matplotlib.pyplot as plt
filename = 'death_valley_2018_simple.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates = []
highs = []
lows = []
for row in reader:
date = datetime.strptime(row[2], '%Y-%m-%d')
try:
high = int(row[4])
low = int(row[5])
except ValueError:
print(f'missing data for {date}')
else:
lows.append(low)
dates.append(date)
highs.append(high)
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.plot(dates, highs, c='red', alpha=0.5)
ax.plot(dates, lows, c='blue', alpha=0.5)
ax.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)
ax.set_title('2018年每日最高温度和最低温度\n 美国加利福利亚死亡谷', fontsize=24)
ax.set_xlabel('', fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel('温度(F)', fontsize=16)
ax.tick_params(axis='both', which='major', labelsize=16)
plt.show()