[Python] Python learning and python Learning
Python learning-visual data operations (1)
GitHub: https://github.com/liqingwen2015/my_data_view
Directory
- Line chart
- Scatter chart
- Random Walk
- Dice Count Probability
- File directory
Line chart
Cube_squares.py
Import matplotlib. pyplot as pltx_values = list (range (1, 5000) y_values = [pow (x, 3) for x in x_values] plt. scatter (x_values, y_values, c = y_values, cmap = plt. cm. blues, edgecolor = 'none', s = 40) # Set the title and style plt. title ("Square Numbers", fontsize = 24) plt. xlabel ("Value", fontsize = 14) plt. ylabel ("Square of Value", fontsize = 14) # Set the size of the scale tag plt. tick_params (axis = 'both ', which = 'major', labelsize = 14) plt. show ()
# Simple line chart
Import matplotlib. pyplot as pltinput_values = [1, 2, 3, 4, 5] squares = [1, 4, 9, 16, 25] # drawing the line width plt. plot (input_values, squares, linewidth = 5) # Set the chart title and add the label plt to the axis. title ("Square Numbers", fontsize = 24) plt. xlabel ("Value", fontsize = 14) plt. ylabel ("Square of Value", fontsize = 14) # Set the size of the scale mark. axis = 'both' indicates that the specified real parameter affects the scale plt on the x and y axes. tick_params (axis = 'both ', labelsize = 14) plt. show ()
# Scatter chart import matplotlib. pyplot as pltx_values = list (range (1, 1001) y_values = [x ** 2 for x in x_values] # c: color # plt. scatter (x_values, y_values, c = 'red', edgecolor = 'none', s = 40) # plt. scatter (x_values, y_values, c = (0, 0, 8), edgecolor = 'none', s = 40) plt. scatter (x_values, y_values, c = y_values, cmap = plt. cm. blues, edgecolor = 'none', s = 40) # Set the title and style plt. title ("Square Numbers", fontsize = 24) plt. xlabel ("Value", fontsize = 14) plt. ylabel ("Square of Value", fontsize = 14) # Set the size of the scale tag plt. tick_params (axis = 'both ', which = 'major', labelsize = 14) plt. show () # Save graph table into plt.savefig('squared_plot.png ', bbox_inches = 'tight ')
From random import choiceclass RandomWalk (): def _ init _ (self, num_points = 5000): # initialize the random Walk attribute self. num_points = num_points # All Random Walks start with (0, 0) self. x_values = [0] self. y_values = [0] def fill_walk (self): # Walk continuously until the list reaches the specified length while len (self. x_values) <self. num_points: x_step = self. get_step (); y_step = self. get_step (); # reject in-situ step if x_step = 0 and y_step = 0: continue # Calculate the x and y values of the next vertex next_x = self. x_values [-1] + x_step next_y = self. y_values [-1] + y_step self. x_values.append (next_x) self. y_values.append (next_y) def get_step (self): # determine the forward direction and the distance forward along this direction. direction = choice ([1,-1]) # randomly select 1 or-1 distance = choice ([0, 1, 2, 3, 4]) # randomly select 0, 1, 2, 3, 4 return direction * distance # positive: Shift right, negative: shift left
Rw_visual.py
Import matplotlib. pyplot as pltfrom Random Walk. random_walk import RandomWalk while True: # create a RandomWalk instance and draw out all its contained vertices rw = RandomWalk (5000) 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 = 1) # Set the size of the drawing window # plt. figure (dpi = 128, figsize = (10, 6) # Highlight the start point and end point 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) # plt. plot (rw. x_values, rw. y_values, linewidth = 10) # Hide the coordinate axis plt. axes (). get_xaxis (). set_visible (False) plt. axes (). get_yaxis (). set_visible (False) plt. show () keep_running = input ("continue? (Y/n): ") if keep_running = 'N': break
Dice Count Probability
Die. py
From random import randintclass Die (): # represents a dice Type def _ init _ (self, num_sides = 6): #6 self. num_sides = num_sides def roll (self): # returns 1 ~ 6 return randint (1, self. num_sides)
Die_visual.py
Import pygalfrom dice. die import Die # create a D6die = Die () results = [] for roll_num in range (1000): result = die. roll () results. append (result) frequencies = [] for value in range (1, die. num_sides + 1): # calculate the number of times that the same value appears. frequency = results. count (value) frequencies. append (frequency) # visualize the result hist = pygal. bar () hist. title = "D6 1000 times:" hist. x_labels = [str (num) for num in range (1, 7)] # ['1', '2', '3', '4', '5 ', '6'] hist. x_title = "result" hist. y_title = "probability" hist. add ('d6 ', frequencies) hist. render_to_file ('images/die_visual.svg ')
Import pygalfrom dice. die import Die # create two D6die_1 = Die () die_2 = Die () results = [] for roll_num in range (1000): result = die_1.roll () + die_2.roll () results. append (result) frequencies = [] max_results = die_1.num_sides + die_2.num_sidesfor value in range (2, max_results + 1): # Calculate the same number of times for a value. frequency = results. count (value) frequencies. append (frequency) # visualize the result hist = pygal. bar () hist. title = "D6 100 times:" hist. x_labels = [str (num) for num in range (1, 13)] # ['1', '2', '3', '4', '5 ', '6', '7', '8', '9', '10', '11', '12'] hist. x_title = "result" hist. y_title = "number of occurrences" hist. add ('d6 + D6 ', frequencies) hist. render_to_file ('images/dice_visual.svg ')
Different_dice.py
Import pygalfrom dice. die import Die # create a D6 and D10die_1 = Die () die_2 = Die (10) results = [] for roll_num in range (5000): result = die_1.roll () + die_2.roll () results. append (result) frequencies = [] max_results = die_1.num_sides + die_2.num_sidesfor value in range (2, max_results + 1): # Calculate the same number of times for a value. frequency = results. count (value) frequencies. append (frequency) # visualize the result hist = pygal. bar () hist. title = "5000" Times: D6 + D10 results. "Hist. x_labels = [str (num) for num in range (2, 17)] hist. x_title = "result" hist. y_title = "repeated times" hist. add ('d6 + d10', frequencies) hist. render_to_file ('images/different_visual.svg ')
File directory
GitHub: https://github.com/liqingwen2015/my_data_view