SoFunction
Updated on 2024-10-29

Python reads csv, Excel files to generate charts

synopsis

This article describes the method to convert a csv or Excel file into a line chart or bar chart by reading its content and writing it to an html file.

1. Reading CSV files

1.1 Generating line graphs

1.1.1 Simple chart generation

The code is as follows

import pandas
import  as plt
# Set the csv file path
file = r'E:\'
# Prevent garbled Chinese characters
['-serif'] = ['Microsoft YaHei']
# Read the contents of a file via pandas
df = pandas.read_csv(file)
# Set the first line of the file to the sequence name
series_names = df .columns
# Plotting document data as line graphs
(df)
(series_names)  # Set the sequence name
('Horizontal axis title')     #Set the name of the horizontal coordinate
('Vertical axis title')     # Set the name of the vertical coordinate
('Chart Title')      # Setting the chart title
# Display the plotted charts on the screen
()
# Save the chart as a png file
# ('')

The results are plotted as follows (the csv file contains 3 columns of data)

1.1.2 Setting up a line chart format

The code is as follows (added to the code in the 1.1.1 catalog: image length and width, markup style, grid lines, horizontal and vertical axis reference lines)

import pandas
import  as plt
# Set the csv file path
file = r'E:\'
# Prevent garbled Chinese characters
['-serif'] = ['Microsoft YaHei']
# Read the contents of a file via pandas
df = pandas.read_csv(file)
# Set the first line of the file to the sequence name
series_names = df .columns
# Define the size of the chart (length 15, height 8), must be placed in the front
(figsize=(15,8))
# Plotting document data as line graphs
(df, marker='o', markersize=3)  #marker sets marker style and size
('Horizontal axis title')   #Set the name of the horizontal coordinate
('Vertical axis title')   # Set the name of the vertical coordinate
('Chart Title')    # Setting the chart title
()              # Show gridlines
(series_names)  # Set the sequence name
(y=2, color='r', linestyle='--')   # Add a horizontal reference line on the y-axis.
(x=1, color='r', linestyle='--')   # Add a vertical reference line on the x-axis.
# Display the plotted charts on the screen
()
# Save the chart as a png file
# ('')

The results are plotted as follows

1.2 Generating bar charts

1.2.1 Simple chart generation

The code is as follows

import pandas as pd
import  as plt
# Read data from CSV file and specify first row as column name
data = pd.read_csv(r'E:\', header=0)
# Prevent garbled Chinese characters
['-serif'] = ['Microsoft YaHei']
# Get the column name and corresponding value
columns = 
values = [0].values
# Plotting bar charts
(columns, values)
('Horizontal axis title')
('Vertical axis title')
('Chart Title')
# Display the plotted charts on the screen
()
# Save the chart as a png file
# ('')

The results are plotted as follows

1.2.2 Formatting bar charts

color code

'b' for blue 'g' for green 'r' for red 'c' for cyan 'm' for magenta 'y' for yellow 'k' for black 'w' for white.

The code is as follows (added to the code in the 1.2.1 directory: image length and width, column width/color, border width/color)

import pandas as pd
import  as plt
# Read data from CSV file and specify first row as column name
data = pd.read_csv(r'E:\', header=0)
# Prevent garbled Chinese characters
['-serif'] = ['Microsoft YaHei']
# Define the size of the chart (length 15, height 8), must be placed in the front
(figsize=(15,8))
# Get the column name and corresponding value
columns = 
values = [0].values
# Plotting bar charts
(columns, values,    # Horizontal axis coordinates
        color='y',          # Set the column color (yellow, see color code)
        width = 0.8,        # Setting the column width
        edgecolor='k',      # Set the column border color (black, see color code)
        linewidth=2         # Setting the column border width
)
('Horizontal axis title')
('Vertical axis title')
('Chart Title')
# Display the plotted charts on the screen
()
# Save the chart as a png file
# ('')

The results are plotted as follows

2. Reading Excel files

2.1 Generating line graphs

2.1.1 Simple chart generation

The code is as follows

import pandas
import  as plt
# Prevent garbled codes
['-serif'] = ['Microsoft YaHei']
# Read the contents of an Excel file
df = pandas.read_excel(r'E:\', sheet_name='Sheet1')
column_names = ()  # Get the name of line 1
excel_rows = [0]            # Get the number of rows
# Set the horizontal axis data, you can get in Excel, here directly traverse the number of rows from the beginning of 1 to the end
x = [i for i in range(1, excel_rows+1)]
# Iterate over the first row of column names
for col_name in column_names:
    # Extract data for drawing by column name
    y = df[col_name].to_numpy()
    # Draw a diagram, execute the horizontal axis coordinates, set the line legend name to the column name of the first row
    (x, y, label=col_name)
# Setting Properties
()  # Show legend title
('Name of the horizontal coordinate')
('Name of vertical coordinate')
('Line Chart Title')
# ('. /') # Save the picture
()  # View Pictures

The results are plotted as follows

2.1.2. Setting up the line chart format

The code is as follows (added to the code in the 2.1.1 catalog: image length and width, markup style, gridlines, horizontal and vertical axis reference lines)

import pandas
import  as plt
# Prevent garbled codes
['-serif'] = ['Microsoft YaHei']
# Read the contents of the Excel file, if reading a csv file, use pandas.read_csv
df = pandas.read_excel(r'E:\', sheet_name='Sheet1')
column_names = ()  #Read the name of line 1
excel_rows = [0]             # of lines read
# Define the size of the chart (length 15, height 8), must be placed in the front
(figsize=(15,8))
# Set the horizontal axis data, you can get in Excel, here directly traverse the number of rows from the beginning of 1 to the end
x = [i for i in range(1, excel_rows+1)]
# Iterate over the first row of column names
for col_name in column_names:
    # Extract data for drawing by column name
    y = df[col_name].to_numpy()
    # Draw a diagram, execute the horizontal axis coordinates, set the line legend name to the column name of the first row
    (x, y, label=col_name, marker='o', markersize=3)    #marker sets marker style and size
# Setting Properties
()  # Show legend title
('Name of the horizontal coordinate')
('Name of vertical coordinate')
('Line Chart Title')
()              # Show gridlines
(y=2, color='r', linestyle='--')   # Add a horizontal reference line on the y-axis
(x=1, color='r', linestyle='--')   # Add a vertical reference line on the x-axis
# ('. /') # Save the picture
()  # View Pictures

The results are plotted as follows

2.2 Generating bar charts

2.2.1 Simple chart generation

The code is as follows (single line of data)

import pandas
import  as plt
# Prevent garbled codes
['-serif'] = ['Microsoft YaHei']
# Read the contents of an Excel file
df = pandas.read_excel(r'E:\', sheet_name='Sheet1')
# Get the name of the first line
column_names = ()
# Remove the first line and transpose the data
data = 
# Create a bar chart, specifying the type of plotting as a bar chart (line: line chart (default), bar: bar chart, barh: horizontal bar chart, hist: histogram, box: box line chart, kde: kernel density estimation chart, density: density chart, area: area chart, scatter: scatter chart, hexbin: Hexbin chart (used to show the distribution of two-dimensional data)
(kind='bar')
# Set the name of the column (rotation rotates the column heading by degrees)
(range(len(column_names)), column_names, rotation=0)
# Add title and axis labels
('Chart Title')
('Horizontal axis title')
('Vertical axis title')
# Display bar graphs
()

The results are plotted as follows

Multiple rows of data need to be added to modify the legend name of the method, the default from 0, set to start from 1. Each legend indicates the number of rows, scale 1: the first row; 2: the second row...

import pandas
import  as plt
# Prevent garbled codes
['-serif'] = ['Microsoft YaHei']
# Read the contents of an Excel file
df = pandas.read_excel(r'E:\', sheet_name='Sheet2')
# Get the name of the first line
column_names = ()
# Remove the first line and transpose the data
data = 
# Create a bar chart, specifying the type of plotting as a bar chart (line: line chart (default), bar: bar chart, barh: horizontal bar chart, hist: histogram, box: box line chart, kde: kernel density estimation chart, density: density chart, area: area chart, scatter: scatter chart, hexbin: Hexbin chart (used to show the distribution of two-dimensional data)
(kind='bar')
# Set the name of the column (rotation rotates the column heading by degrees)
(range(len(column_names)), column_names, rotation=0)
# Add title and axis labels
('Chart Title')
('Horizontal axis title')
('Vertical axis title')
# Modify the legend name (start with 1, 1 means row 1 data...)
handles, labels = ().get_legend_handles_labels()
labels = [int(label)+1 for label in labels]
(handles, labels)
# Display bar graphs
()

2.2.2 Formatting bar charts

color code

'b' for blue 'g' for green 'r' for red 'c' for cyan 'm' for magenta 'y' for yellow 'k' for black 'w' for white.

The code is as follows (added to the code in the 2.2.1 directory: image length and width, column width/color, border width/color)

import pandas
import  as plt
# Prevent garbled codes
['-serif'] = ['Microsoft YaHei']
# Read the contents of an Excel file
df = pandas.read_excel(r'E:\', sheet_name='Sheet1')
# Set the image size (length 15, width 8)
fig = (figsize=(15, 8))
ax = fig.add_subplot()
# Get the name of the first line
column_names = ()
# Remove the first line and transpose the data
data = 
# Create bar charts
(
    kind='bar', # Specify the type of plotting as a histogram (line: line plot (default), bar: bar chart, barh: horizontal bar chart, hist: histogram, box: box line plot, kde: kernel density estimation plot, density: density plot, area: area plot, scatter: scatter plot, hexbin: Hexbin plot (used to show the two-dimensional data distribution)
    color='y',  # Set the column color (yellow, see color code)
    width=0.8,  # Set the column width
    edgecolor='k',  # Set the column border color (black, see color code)
    linewidth=2,    # Setting the column border width
    ax=ax           #Set the image size
)
# Set the name of the column (rotation rotates the column heading by degrees)
(range(len(column_names)), column_names, rotation=0)
# Add title and axis labels
('Chart Title')
('Horizontal axis title')
('Vertical axis title')
# Display bar graphs
()

The results are plotted as follows

3. Write the generated line graph into the html file.

3.1 Write directly to the image

Using MarkupPy to write images to html files

from MarkupPy import markup
# Add images, set length and width
page = ()
('<img src="./" alt="csv-generated line graphs" width="800" height="500">')
# Write the file
with open('./', 'w') as file:
    (str(page))

The code is as follows (using the csv-generated line chart as an example)

import pandas
import  as plt
from MarkupPy import markup
# Set the csv file path
file = r'E:\'
# Prevent garbled Chinese characters
['-serif'] = ['Microsoft YaHei']
# Read the contents of a file via pandas
df = pandas.read_csv(file)
# Set the first line of the file to the sequence name
series_names = df .columns
# Plotting document data as line graphs
(df)
(series_names)  # Set the sequence name
('Horizontal axis title')     #Set the name of the horizontal coordinate
('Vertical axis title')     # Set the name of the vertical coordinate
('Chart Title')      # Setting the chart title
# Save the chart as a png file
('./')
# Add a picture
page = ()
('<img src="./" alt="csv-generated line graphs" width="800" height="500">')
# Write the file
with open('./', 'w') as file:
    (str(page))

The results are as follows

3.2 Adding text descriptions

For a description of the file, see the detailed usage of MarkupPy.

https:///python/

to this article on Python read csv, Excel file to generate charts to this article, more related Python read csv to generate charts content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!