"Python" Scientific Computing Special _ Scientific Drawing Library matplotlib learning (bottom)

Source: Internet
Author: User

Basic reading of CSV file and drawing pie chart

Since there is no prior experience of actual processing, this procedure is still worth seeing, involving the basic methods of working with tabular data:

Import Matplotlib.pyplot as Pltimport pandas as pd# csv read file data = pd.read_csv (' outorder.csv ', encoding= ' gb2312 ') # Each column is compatible with the NumPy method A = data[' way '].values# gets the various possible contents of this column TypeName = []for i in a: if I is not in    TypeName:        typename.append (i) # Get each possible number of Typenum = []for i in TypeName:    typenum.append (SUM (a==i)) print (typenum) # Draw pie chart Plt.axis (' equal ') Plt.pie ( Typenum,labels=typename,shadow=true,        labeldistance=1.1,autopct= '%3.1f%% ',            startangle=0,pctdistance=0.8 ) Plt.legend () plt.show ()

Added time-processing charts, a little more complicated

There are two places worth noting:

    • The two methods of converting the STR string to a DateTime object (DateTime package and pandas built-in integration)
    • How to draw a double-ordinate graph
From matplotlib import Pyplot as Pltimport pandas as Pdimport numpy as Npimport datetimedata = pd.read_csv (' Outorder.csv ',  encoding= ' gb2312 ') def ToDT (a): Return Datetime.datetime.strptime (A, '%m/%d/%y%h:%m ') # Note time series string processing # Convert str into datetime format a = Data[u ' time '].apply (ToDT) # <-------display using datetime processing time string # a = Pd.to_datetime (data[u ' time ') # <-------pandas inside The processing time string method, and the above equivalent print (a) # x is used to save the month, Y to save the amount, Z to save the number of x=[]; Y=[]; Z=[]for i in range: Start_time = Datetime.datetime (i+1, 1) If i + 2 > 12:end_time = Datetime.da Tetime (1, 1) else:end_time = Datetime.datetime (i+2, 1) idx = Np.where ((a>start_time) & (A&L T;end_time) # Np.where Returns a single-element index tuple containing an array: (array arrays,), so add [0] If Len (idx[0]) >0:x.append (i + 1) y.appe nd (SUM (data[' amount '].values[idx[0])) z.append (Len (idx[0])) width = 0.25fig, ax1 = Plt.subplots () ax1.bar (Np.array (X)-wi DTH, Y, width, facecolor= ' #9999ff ', edgecolor= ' white ') ax1.set_ylabel (' Money ', ' color= ' B ') ax1.set_xlabel (' month ') # Ax1.bar (Np.array (X) +1, Z, width, facecolor= ' #ff9999 ', edgecolor= ' white ') # Common Companion Graph drawing method ax2 = Ax1.twinx () # <-------Companion Diagram drawing method (using the same horizontal axis, but not with the longitudinal axes) ax2.bar (X, Z, width, facecolor= ' #ff9999 ', edgecolor= ' white ' ) Ax2.set_ylabel (' People num ', color= ' R ') Plt.grid (True) plt.show ()

Output Image:


Output plots that do not use a double ordinate axis:

"Python" Scientific Computing Special _ Scientific Drawing Library matplotlib learning (bottom)

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.