SoFunction
Updated on 2024-10-26

Python read Excel tables, and draw line graphs and bar charts at the same time method

Today to share with you a Python read Excel tables, while using the values in the table to draw bar charts and line graphs, here only a few lines of code can be real.

First of all, we need to install an Excel library xlrd, this is very simple, after the installation of Python directly under the DOS command type pip install xlrd, can be installed successfully, if it still does not work, type Python -m pip install xlrd. the back of the complete code and screenshots will be attached:

This line of code is what reads the local Excel file:

data = xlrd.open_workbook(r'C:\\Users\\ASUS\\Desktop\\txt1\\python3\\')
table = ()[0]

The next step is to read the values in the columns, i.e., the data of the objects in the columns: this is my example of reading the third column of values

table.col_values(2)

I use here is pyechats library drawing, so here also need to install; pip install pyechats, at the same time we need numpy library in the linspace, equal interval value, because the data in Excel is too large is that may produce the broadcast scale out of range this scale.

t=(1,296,len(y))#value in equal intervals

The next step is to add the data for the drawing:

("Blog post readership line graph presentation",t,y,is_more_utils=True)

Save to a local HTML format file:

(r"C:/Users/ASUS/Desktop/txt1/")#Save to local

Here is the complete code:

# coding:utf-8
# Import libraries that read Excel
import xlrd
# Import the path where you need to read the Excel sheet
data = xlrd.open_workbook(r'C:\\Users\\ASUS\\Desktop\\txt1\\python3\\')
table = ()[0]
y=''
# Stores the value of a column into a string
y=table.col_values(2)# Read the value of the column
# Import the pyechats library
from pyecharts import Bar
import numpy as np
t=(1,296,len(y))# Equally spaced values
bar=Bar("Article readership display","The statistics are as follows.")# Main Subtitle
("Blog post readership line graph presentation",t,y,is_more_utils=True)# Title
bar.show_config()# Show HTML source code
(r"C:/Users/ASUS/Desktop/txt1/")#SaveLocal

Results Showcase:

Python 读取Excel表格

Python 画折线图和柱状图

Well, here it is, for Python I am also a beginner, there is something bad hope to teach one or two!

Above this Python read Excel tables, and at the same time draw line graphs and bar charts is the method I shared with you all, I hope to give you a reference, and I hope you support me more.