def cont(): rw = RandomWalk() rw.fill_walk() plt.plot(rw.x ,rw.y, c = (0.3, 0.6, 0.6)) plt.show()
# CH15 Exercise 15-10 # # random_pygal.py # # Use pygal to vizualize a random walk. Pygal is the visualization library that # contains histograms and other plots import pygal from random_walk import RandomWalk # Create new random walk with just 1000 points and run fill_walk() method new_walk = RandomWalk(1000) new_walk.fill_walk() # Create list of tuples for xy_chart using a function and list comprehension def merged(list1, list2): merged_list = [(list1[i], list2[i]) for i in range(0, len(list1))] return merged_list # Merge x and y lists from new_walk instance points = merged(new_walk.x_values, new_walk.y_values) # Correctly color first and last points of the merged list. I can't believe how # much work goes into this crap. This is why we use matplotlib... formatted_points = [] for item in points: if item == points[0]: formatted_points.append({"value": item, "color": "green"}) elif item == points[-1]: formatted_points.append({"value": item, "color": "blue"})
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: rw = RandomWalk() rw.fill_walk() #num_points个点的数据全部载入 point_numbers = list(range(rw.num_points)) print(point_numbers) print(rw.x_values) print(rw.y_values) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor='none', s=15) #c颜色深浅,越大越深,cmap为色系,c和cmp联用 plt.scatter(rw.x_values[0], rw.y_values[0], c='green', edgecolor='none', s=100) #起点 plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none', s=100) #终点 plt.show() keep_running = input("make another walk?(y/n):")
import matplotlib.pyplot as plt from random_walk import RandomWalk #创建一个Randomalk实例,并将其包含的点描绘出来 rw = RandomWalk() #将Randomalk实例保存至rw rw.fill_walk() #调用fill_walk() plt.scatter(rw.x_values, rw.y_values, s=15) #将随机漫步包含的x与y值传递 plt.show()
from random_walk import RandomWalk from matplotlib import pyplot as plt # create an object steps = RandomWalk() steps.fill_walk() # create a list representing point sequence point_numbers = list(range(steps.num_points)) # plot random path plt.scatter(steps.x_values, steps.y_values, s=1, c=point_numbers, cmap=plt.cm.Greens) # plot origin plt.scatter(0, 0, s=20) plt.show()
import matplotlib.pyplot as plt from random_walk import RandomWalk # Keep making new walks, as long as the program is active. while True: # Make a random walk, and plot the points. # Adding plot points rw= RandomWalk(50000) # call RandomWalk class rw.fill_walk() # call fill_walk function # Set the size of the plotting window. plt.figure(dpi=128, figsize=(10,6)) # Plot and color the random walking point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues , edgecolor='none', s=2) # Emphasize the first and last points. plt.scatter(0,0, c='green', edgecolor='none' , s=50) plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none' ,s=50) # Remove the axes. plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() # ploting
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: # Make a random walk, and plot the points. rw = RandomWalk() rw.fill_walk() # Set the size of tthe plotting window. plt.figure(figsize=(10, 6)) point_numbers = list(range(rw.num_points)) plt.plot(rw.x_values, rw.y_values, linewidth=5) # Emphasize the first and las points. plt.scatter(0, 0, c="green", edgecolors="none", s=100) plt.scatter(rw.x_values[-1], rw.y_values[-1], c="red", edgecolors="none", s=100) # Remove the axes. plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() keep_running = input("Make anotther walk (y/n): ") if keep_running == 'n': break
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: a = RandomWalk(50000) a.fill_walk() point_numbers = list(range(a.num_point)) plt.scatter(a.x_points, a.y_points, s=1, c=point_numbers, cmap=plt.cm.Blues) plt.scatter(0, 0, c='green', s=100) plt.scatter(a.x_points[-1], a.y_points[-1], c='red', s=100) plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) # plt.figure(figsize=(5, 3)) plt.show() keep_running = input("Make another walk? (y/n): ") if keep_running == 'n': break
import matplotlib.pyplot as plt from random_walk import RandomWalk # Make a random walk and plot the points while True: walk = RandomWalk(50000) walk.fill_walk() point_numbers = list(range(walk.num_points)) # Set the size of the plot window plt.figure(dpi=128, figsize=(10, 6)) plt.scatter(walk.x_values, walk.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolors='none', s=1) plt.scatter(0, 0, c='green', edgecolors='none', s=100) plt.scatter(walk.x_values[-1], walk.y_values[-1], c='red', edgecolors='none', s=100) # Remove the axes plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() keep_running = input('Make another walk? (y/n) ') if keep_running.lower() == 'n': break
import matplotlib.pyplot as plt from random_walk import RandomWalk #创建一个RandomWalk实例 rw = RandomWalk(); rw.fill_walk(); point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolors='none',s=10); plt.show();
# -*- coding: utf-8 -*- """ Created on Tue Mar 3 14:31:31 2020 @author: jwitherspoon """ import pygal from random_walk import RandomWalk rw1 = RandomWalk() rw2 = RandomWalk() rw3 = RandomWalk() rw1.fill_walk() rw2.fill_walk() rw3.fill_walk() xy1 = [i for i in zip(rw1.x_values, rw1.y_values)] xy2 = [i for i in zip(rw2.x_values, rw2.y_values)] xy3 = [i for i in zip(rw3.x_values, rw3.y_values)] scatter_chart = pygal.XY(stroke=False) scatter_chart.title = 'Random Walk' scatter_chart.add('Step1', xy1) scatter_chart.add('Step2', xy2) scatter_chart.add('Step3', xy3) scatter_chart.render_to_file('randomwalk_xy.svg')
#coding=utf-8 from random_walk import RandomWalk import matplotlib.pyplot as plt #while True: rm = RandomWalk(50000) rm.fill_walk() point_numbers = list(range(rm.num_points)) plt.scatter(rm.x_values, rm.y_values, c=point_numbers, cmap=plt.cm.Blues, s=5, edgecolor='none') plt.scatter(0, 0, c='green', edgecolor='none', s=100) plt.scatter(rm.x_values[-1], rm.y_values[-1], c='red', edgecolor='none', s=100) # 隐藏坐标轴 plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() # keep_running = raw_input("Make another walk? (y/n): ") # if keep_running == 'n': # break
"""This module contains a logic that allows to build random walks visualizations""" # import import matplotlib.pyplot as plt from random_walk import RandomWalk # create and visualize random walk data set while True: random_walk = RandomWalk( 50000) # random walk data set will consist of 50000 points random_walk.fill_walk() points_numbers = list(range(random_walk.num_points)) # set plot size plt.figure(figsize=(10, 6)) # in inches # set plot title plt.title('Random walk visualization: {} points'.format( len(points_numbers)), loc='center') # plot random walk plt.scatter(random_walk.x_values, random_walk.y_values, c=points_numbers, cmap=plt.cm.Blues, edgecolor='none', s=1) # highlight first and last points in the random walk plt.scatter(0, 0, c='green', edgecolor='none', s=50, label='start point') plt.scatter(random_walk.x_values[-1], random_walk.y_values[-1],
# Use the RandomWalk class. from random_walk import RandomWalk # Keep making random walks as long as the program is active. print("Enter 'q' to quit at any time.") while True: # Make a walk and plot the points steps = input("How many steps would you like to take? ") if steps == 'q': break my_walk = RandomWalk(int(steps)) my_walk.fill_walk() my_walk.plot_walk()
from random_walk import RandomWalk import pygal # Generate a random walk dataset with 10,000 points rw_dataset = RandomWalk(10000) rw_dataset.fill_walk() # Helper function for generating xy_pairs from random walk dataset def get_xy_pairs(random_walk, start, end): """Generate a list comprehension from the xy-pairings in *random_walk* in _range(*start*, *end*)_""" xy_pairs = [(random_walk.x_values[n], random_walk.y_values[n]) for n in range(start, end)] return xy_pairs # Display the dataset as a scatter plot and a line graph line_chart = pygal.XY() line_chart.title = "Data from a random walk of 10,000 steps" #line_chart.x_labels line_chart.add('Steps 0-999', get_xy_pairs(rw_dataset, 0, 1000)) line_chart.add('Steps 1,000-1,999', get_xy_pairs(rw_dataset, 1000, 2000)) line_chart.add('Steps 2,000-2,999', get_xy_pairs(rw_dataset, 2000, 3000)) line_chart.add('Steps 3,000-3,999', get_xy_pairs(rw_dataset, 3000, 4000)) line_chart.add('Steps 4,000-4,999', get_xy_pairs(rw_dataset, 4000, 5000)) line_chart.add('Steps 5,000-5,999', get_xy_pairs(rw_dataset, 5000, 6000)) line_chart.add('Steps 6,000-6,999', get_xy_pairs(rw_dataset, 6000, 7000)) line_chart.add('Steps 7,000-7,999', get_xy_pairs(rw_dataset, 7000, 8000)) line_chart.add('Steps 8,000-8,999', get_xy_pairs(rw_dataset, 8000, 9000)) line_chart.add('Steps 9,000-9,999', get_xy_pairs(rw_dataset, 9000, 10000)) line_chart.render_to_file('rw_walk_test.svg')
#multiple random walk and styling import matplotlib.pyplot as plt from random_walk import RandomWalk while True: mrw = RandomWalk(50000) #we create a random walk and store it in rw mrw.fill_walk() plt.figure(dpi=128, figsize=(10, 6)) #it control width, height, background, resolution point_number = list(range(mrw.num_points)) plt.scatter(mrw.x_values, mrw.y_values, c=point_number, cmap=plt.cm.Reds, edgecolor='none', s=10) #Emphasis the first and last points plt.scatter(0, 0, c='green', edgecolor='none', s=100) #starting point in Green plt.scatter(mrw.x_values[-1], mrw.y_values[-1], c='red', edgecolors='none', s=100) #ending point in Red plt.savefig('mrw.png', bbox_inches='tight') plt.show() keep_running = input("Make Another walk (Y/N): ") if keep_running == 'n': break
import matplotlib.pyplot as plt from random_walk import RandomWalk # Use RandomWalk class to generate this plot mol = RandomWalk() mol.fill_walk() # Set the size of the plotting window. plt.figure(figsize=(12, 6)) point_numbers = list(range(mol.num_points)) plt.plot(mol.x_values, mol.y_values, c='purple', linewidth=1.2) # Emphasize the first and last points. plt.scatter(0, 0, c='green', edgecolors='none', s=60) plt.scatter(mol.x_values[-1], mol.y_values[-1], c='red', edgecolors='none', s=60) # Remove the axes. plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show()
import matplotlib.pyplot as plt from statistics import median from random_walk import RandomWalk while True: #keep making new walks as long as the program is active #make a random walk and plot the points rw = RandomWalk( 10000 ) #the input here is basically running the class with this number of points since it #is feeding the 'point numbers' attribute for the class rw.fill_walk(rw.get_step) point_numbers = list( range(rw.num_points) ) #make a list from 0-the number of points we set in the call to the class plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Reds, s=5) #we plot the scatter points and we use #the 'point number' to gradually darken the scatter as the number of points plotted increases to show the path of the walk #settings for the axes and title of graph plt.title('Random Walk', fontsize=14) plt.xlabel('X Values', fontsize=10) plt.ylabel('Y Values', fontsize=10) #this is to hide the axes to get a pure view of our scatter #plt.axes().get_xaxis().set_visible(False) #plt.axes().get_yaxis().set_visible(False)
import matplotlib.pyplot as plt from random_walk import RandomWalk #传进一个RandomWalk实例,并将其包含的点都绘制出来 rw = RandomWalk(5000) rw.fill_walk([1], [1, 2, 3, 4, 5, 6, 7, 8], [1, -1]) point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, s=10, edgecolors='none', c=point_numbers, cmap=plt.cm.Blues) #plt.scatter(rw.x_values, rw.y_values, s=10, edgecolors='none', c='red') plt.title("Random Walk") #隐藏坐标轴 # plt.axes().get_xaxis().set_visible(False) # plt.axes().get_yaxis().set_visible(False) plt.show()
import matplotlib.pyplot as plt from random_walk import RandomWalk #只要程序活跃就不断的模拟随机漫步 while True: # 创建一个RandomWalk实例, 并将其包含的点都绘制出来 rw = RandomWalk(50000) rw.fill_walk() #生成随机漫步坐标列表 #设置绘图窗口的分辨率与尺寸 plt.figure( dpi=128, figsize=(10, 6)) #生成一个有num_points数目个数的列表 point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor='none', s=1) #绘制散点图,按顺序着色深浅,删除黑色轮廓,大小为10 #突出起点和终点 plt.scatter(0, 0, c='green', edgecolor='none', s=50) plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none', s=50) #显示列表中的最后一个点 #隐藏坐标轴 plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() #判断 keep_running = input("Make it running again? (y/n)") #输入语句 if keep_running == 'n': #如果是'n'就结束循环
import matplotlib.pyplot as plt from random_walk import RandomWalk while True: rw = RandomWalk(50000) rw.fill_walk() plt.figure(figsize=(10, 6)) point_numbers = list(range(rw.num_points)) #Emphasize the first and last point #plt.scatter(0, 0, c='green', edgecolors='none', s=100) #plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none', s=100) #Remove the axes. #plt.axes().get_xaxis().set_visible(False) #plt.axes().get_yaxis().set_visible(False) plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolor='none', s=15) plt.scatter(rw.x_values, rw.y_values, s=15) plt.show() keep_running = raw_input("Make another walk? (y/n): ") if keep_running == 'n': break
import matplotlib.pyplot as plt from random_walk import RandomWalk grain_path = RandomWalk(5000) grain_path.fill_walk() point_numbers = range(grain_path.num_points) plt.style.use('classic') fig, ax = plt.subplots(figsize=(15, 9)) ax.plot(grain_path.x_values, grain_path.y_values, linewidth=1, c='blue') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.savefig('pollen_grain.png', bbox_inches='tight') plt.show()