[Python] Python Learning-visualizing data manipulation (i)

Source: Internet
Author: User

Python Learning-visualizing data manipulation (i)

Github:https://github.com/liqingwen2015/my_data_view

Directory
    • Line chart
    • Scatter chart
    • Random Walks
    • Dice count probability
    • File directory

Line chart

cube_squares.py

ImportMatplotlib.pyplot as Pltx_values=list (Range (1, 5000)) Y_values=[pow (x, 3) forXinchX_values]plt.scatter (X_values, y_values, C=y_values, Cmap=plt.cm.blues, edgecolor='None', s=40)#set the title and stylePlt.title ("Square Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.ylabel ("Square of Value", fontsize=14)#set the size of the tick markPlt.tick_params (axis='both', which='Major', labelsize=14) plt.show ()

mpl_squares.py

# Simple Line Chart
ImportMatplotlib.pyplot as Pltinput_values=[1, 2, 3, 4, 5]squares= [1, 4, 9, 16, 25]#draw the thickness of a linePlt.plot (Input_values, squares, linewidth=5)#set the chart title and label The axesPlt.title ("Square Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.ylabel ("Square of Value", fontsize=14)#sets the size of the tick mark, axis= ' Both ' indicates that the specified argument affects the scale on the x-axis and the y-axisPlt.tick_params (axis='both', labelsize=14) plt.show ()

Scatter chart

scatter_squares.py

#Scatter ChartImportMatplotlib.pyplot as Pltx_values= List (range (1, 1001)) Y_values= [x**2 forXinchX_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 stylePlt.title ("Square Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.ylabel ("Square of Value", fontsize=14)#set the size of the tick markPlt.tick_params (axis='both', which='Major', labelsize=14) plt.show ()#Save Chart#plt.savefig (' squared_plot.png ', bbox_inches= ' tight ')

Random Walks

random_walk.py

 fromRandomImportChoiceclassRandomwalk ():def __init__(Self, num_points=5000):        #Initialize the attributes of a random walkSelf.num_points =num_points#all random walks start at (0, 0)Self.x_values =[0] self.y_values=[0]defFill_walk (self):#walk continuously until the column is expressed to the specified length         whileLen (self.x_values) <Self.num_points:x_step=Self.get_step (); Y_step=Self.get_step (); #refusing to tread            ifX_step = = 0 andY_step = =0:Continue            #calculates the x and Y values of the next pointnext_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)defGet_step (self):#to determine the direction of the move and the distance in that direction.Direction = Choice ([1,-1])#randomly select 1 or-1Distance = Choice ([0, 1, 2, 3, 4])#randomly selected 0, 1, 2, 3, 4        returnDirection * Distance#positive: Shift right, negative: shift left

rw_visual.py

ImportMatplotlib.pyplot as Plt fromRandom Stroll. Random_walkImportRandomwalk whileTrue:#Create a Randomwalk instance and draw the points it containsRW = 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= (6))    #highlight start and end pointsPlt.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 Axesplt.axes (). Get_xaxis (). Set_visible (False) Plt.axes (). Get_yaxis (). Set_visible (False) Plt.show () Keep_runni Ng= Input ("go ahead? (y/n):")    ifKeep_running = ='N':         Break

Dice count probability

die.py

 from Import Randint class Die ():     # represents a dice class    def __init__ (Self, num_sides=6):         # 6 sides        Self.num_sides = num_sides    def roll (self):        #  return to 1~6         return randint (1, self.num_sides)

  

die_visual.py

ImportPygal fromDie.Import die#Create a D6Die =Die () results= [] forRoll_numinchRange (1000): Result=Die.roll () results.append (result) frequencies= [] forValueinchRange (1, die.num_sides+1):    #calculate the same number of occurrences of a valuefrequency =Results.count (value) frequencies.append (frequency)#Visualize the resultshist =Pygal. Bar () Hist.title="D6 1000 times:"Hist.x_labels= [STR (num) forNuminchRange (1, 7)]#[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']Hist.x_title ="Results"Hist.y_title="probability"Hist.add ('D6', frequencies) Hist.render_to_file ('Images/die_visual.svg')

dice_visual.py

ImportPygal fromDie.Import die#creation of 2 D6Die_1 =Die () die_2=Die () results= [] forRoll_numinchRange (1000): Result= Die_1.roll () +Die_2.roll () results.append (result) frequencies=[]max_results= Die_1.num_sides +Die_2.num_sides forValueinchRange (2, max_results+1):    #calculate the same number of occurrences of a valuefrequency =Results.count (value) frequencies.append (frequency)#Visualize the resultshist =Pygal. Bar () Hist.title="D6 100 times:"Hist.x_labels= [STR (num) forNuminchRange (1, 13)]#[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', ' One ' , ' one ']Hist.x_title ="Results"Hist.y_title="Number of occurrences"Hist.add ('D6 + D6', frequencies) Hist.render_to_file ('Images/dice_visual.svg')

different_dice.py

ImportPygal fromDie.Import die#Create a D6 and D10Die_1 =Die () die_2= Die (10) Results= [] forRoll_numinchRange (5000): Result= Die_1.roll () +Die_2.roll () results.append (result) frequencies=[]max_results= Die_1.num_sides +Die_2.num_sides forValueinchRange (2, max_results+1):    #calculate the same number of occurrences of a valuefrequency =Results.count (value) frequencies.append (frequency)#Visualize the resultshist =Pygal. Bar () Hist.title="5,000 Plays: D6 + D10 results. "Hist.x_labels= [STR (num) forNuminchRange (2, 17)]hist.x_title="Results"Hist.y_title="number of recurring occurrences"Hist.add ('D6 + D10', frequencies) Hist.render_to_file ('Images/different_visual.svg')

File directory

Github:https://github.com/liqingwen2015/my_data_view

[Python] Python Learning-visualizing data manipulation (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.