SoFunction
Updated on 2024-11-16

Plotting common charts with python

This article introduces if you use python summary of commonly used charts, compared with Excel's point-and-click operation, with python charts appear to be more cumbersome, especially mentioning the processing of raw data. But both in the process of drawing charts in the same idea, Excel can be done in most of the work python can also do. In order to more clearly illustrate the use of python charting process, we summarize the chart of the code in the notes, explaining the specific role of each line of code. And at the end of the article gives the custom fonts and chart color matching table.

preliminary

import numpy as np
import pandas as pd
# Import chart libraries for charting
import  as plt
loandata=(pd.read_excel('loan_data.xlsx'))

line graph

# Set date field issue_d as loandata data table index field
loandata = loandata.set_index('issue_d')
# Average loan amount loan_amnt by month, fill null with 0
loan_plot=loandata['loan_amnt'].resample('M').fillna(0)
# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
# Create a one-dimensional array assigned to a
a=([1,2,3,4,5,6,7,8,9,10,11,12])
#Create a line chart with a data source of average loans by month, marker points, marker line style, line width, marker point color and transparency
(loan_plot,'g^',loan_plot,'g-',color='#99CC01',linewidth=3,markeredgewidth=3,markeredgecolor='#99CC01',alpha=0.8)
# Add x-axis labels
('Month')
#add y weekly hashtag
('Loan amount')
# Add chart title
('Distribution of loan amounts by month')
# Add chart gridlines, set the gridline color, line shape, width and transparency.
( color='#95a5a6',linestyle='--', linewidth=1 ,axis='y',alpha=0.4)
#Set up data classification names
(a, ('January','February','March','April','May','June','July','August','September','October','November','December') )
# Output charts
()

histogram

# Summarize loan amounts by user level grade field
loan_grade=('grade')['loan_amnt'].agg(sum)
# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
# Create a one-dimensional array assigned to a
a=([1,2,3,4,5,6])
# Create a bar chart with a data source of loan amounts aggregated by user level, set the color, transparency and outer border colors
([1,2,3,4,5,6],loan_grade,color='#99CC01',alpha=0.8,align='center',edgecolor='white')
# Setting the x-axis labels
('User level')
#Setupy Weekly Label
('Loan amount')
# Setting the chart title
('Distribution of Loan Amounts by User Level')
# Setting the legend text and position in the chart
(['Loan amount'], loc='upper right')
# Set the color, style, size and transparency of the background gridlines
(color='#95a5a6',linestyle='--', linewidth=1,axis='y',alpha=0.4)
#Set up data classification names
(a,('Grade A','Class B','Class C','Class D','Class E','Class F'))
#Display Chart
()

bar chart

# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
# Create a one-dimensional array assigned to a
a=([1,2,3,4,5,6])
#Create a bar chart with the data source as a summary of tiered loan amounts, set the color, transparency and chart borders
([1,2,3,4,5,6],loan_grade,color='#99CC01',alpha=0.8,align='center',edgecolor='white')
# Add x-axis title
('Loan amount')
# Add y-axis title
('User level')
# Add chart title
('Distribution of Loan Amounts by User Level')
# Add a legend and set the display position in the chart
(['Loan amount'], loc='upper right')
# Set the color, style, size and transparency of the background gridlines
(color='#95a5a6',linestyle='--', linewidth=1,axis='y',alpha=0.4)
#Set up data classification names
(a,('Grade A','Class B','Class C','Class D','Class E','Class F'))
#Display Chart
()

pie chart

# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
# Set the color of each data category in the pie chart
colors = ["#99CC01","#FFFF01","#0000FE","#FE0000","#A6A6A6","#D9E021"]
# Set the name of each data category in the pie chart
name=['Grade A', 'Class B', 'Class C', 'Class D', 'Class E','Class F']
#Create pie charts, set category labels, colors and chart start position, etc.
(loan_grade,labels=name,colors=colors,explode=(0, 0, 0.15, 0, 0, 0),startangle=60,autopct='%1.1f%%')
# Add chart title
('Percentage of loan amounts for different user levels')
# Add a legend and set the display position
(['Grade A','Class B','Class C','Class D','Class E','Class F'], loc='upper left')
#Display Chart
()

scatterplot

# Loan amounts are summarized by month and nulls are filled with zeroes
loan_x=loandata['loan_amnt'].resample('M',how=sum).fillna(0)
# Summarize interest amounts on a monthly basis and fill in the blanks with zeros
loan_y=loandata['total_rec_int'].resample('M',how=sum).fillna(0)
# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
#Create a scatterplot with the loan amount as x and the interest amount as y, set the color, marker style and transparency, etc.
(loan_x,loan_y,60,color='white',marker='o',edgecolors='#0D8ECF',linewidth=3,alpha=0.8)
# Add x-axis title
('Loan amount')
# Add y-axis title
('Interest income')
# Add chart title
('Loan amounts and interest income')
# Set the color, style, size and transparency of the background gridlines
(color='#95a5a6',linestyle='--', linewidth=1,axis='both',alpha=0.4)
#Display Chart
()

bubble chart

# Summarize loan amounts and interest on a monthly basis
loan_x=loandata['loan_amnt'].resample('M',how=sum).fillna(0)
loan_y=loandata['total_rec_int'].resample('M',how=sum).fillna(0)
loan_z=loandata['total_rec_int'].resample('M',how=sum).fillna(0)
# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
#Set the bubble map color
colors = ["#99CC01","#FFFF01","#0000FE","#FE0000","#A6A6A6","#D9E021",'#FFF16E','#0D8ECF','#FA4D3D','#D2D2D2','#FFDE45','#9b59b6']
# Create a bubble chart loan amount as x and interest amount as y. Also set the interest amount as the bubble size and set the color transparency etc.
(loan_x,loan_y,s=loan_z,color=colors,alpha=0.6)
# Add x-axis title
('Loan amount')
# Add y-axis title
('Interest income')
# Add chart title
('Loan amounts and interest income')
# Set the color, style, size and transparency of the background gridlines
(color='#95a5a6',linestyle='--', linewidth=1,axis='both',alpha=0.4)
#Display Chart
()

box plot (math.)

# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
#Create a box-and-line chart, with the data source being the loan source, set to display horizontally
(loandata['loan_amnt'],1,'rs',vert=False)
# Add x-axis title
('Loan amount')
# Add chart title
('Distribution of loan amounts')
# Set the color, style, size and transparency of the background gridlines
(color='#95a5a6',linestyle='--', linewidth=1,axis='both',alpha=0.4)
#Display Chart
()

bar chart

# Charts and graphs are in Mandarin Fine Black, font size 15
('font', family='STXihei', size=15)
# Create a histogram, the data source is the loan amount, divide the data into 8 equal parts to display, set the color and display mode, transparency, etc.
(loandata['loan_amnt'],8,normed=1, histtype='stepfilled',facecolor='#99CC01', rwidth=0.9,alpha=0.6,edgecolor='white')
# Add x-axis title
('Loan amount')
# Add y-axis title
('Probability')
# Add chart title
('Loan amount probability density')
# Set the color, style, size and transparency of the background gridlines
(color='#95a5a6',linestyle='--', linewidth=1,axis='y',alpha=0.4)
#Display Chart
()

Custom fonts and color schemes

The font used in the chart can be changed by replacing what follows family= with the font name below to change the font displayed in the chart.

For the colors in the chart, you can either use the color name directly, or you can use an abbreviation to set the color used in the chart. In this article, instead of using the default color, we used a custom color.

Customize the color code of the color, in this article we use the Hex color code, the following gives the correspondence between Hex and RGB, and the corresponding color. You can use the following Hex color code to replace the color of the chart in this article.