The following will introduce in detail how to use Pythonpyecharts
、matplotlib
andseaborn
Three libraries read data from Excel and draw histograms (Histogram). The histogram is used to show the data distribution, and the core is to count the frequency or frequency of different intervals. Each library's code example contains detailed descriptions of core classes, functions, and properties.
1. Use pyecharts to draw histograms
pyecharts
It is an interactive visualization library based on ECharts, but the frequency distribution of the histogram needs to be manually calculated (because there is no built-in histogram function).
Code example:
import pandas as pd import numpy as np from import Bar from pyecharts import options as opts # Read Excel datadf = pd.read_excel("") data = df["Numerical column"].tolist() # Assume that the data column is named "numerical column"# Calculate the interval and frequency of the histogramcounts, bins = (data, bins=10) # 10 intervalsx_labels = [f"{bins[i]:.1f}-{bins[i+1]:.1f}" for i in range(len(bins)-1)] # Create a bar chart (simulate histogram)bar = Bar() bar.add_xaxis(x_labels) bar.add_yaxis("Frequency", ()) # Configure global optionsbar.set_global_opts( title_opts=(title="Numerical distribution histogram"), xaxis_opts=(name="Interval"), yaxis_opts=(name="Frequency"), toolbox_opts=() ) # Render as HTML file("pyecharts_histogram.html")
Core classes and functions:
-
Bar()
: Bar chart object, used to simulate histograms. -
()
: Calculate the frequency and interval of histogram (bins
Specify the number of intervals). -
add_xaxis()
/add_yaxis()
: Add X/Y axis data. -
set_global_opts()
: Configure title, axis name, etc.
2. Use matplotlib to draw histograms
matplotlib
It is a basic drawing library with built-inhist()
The function directly draws the histogram.
Code example:
import pandas as pd import as plt # Read Excel datadf = pd.read_excel("") data = df["Numerical column"] # Create canvas and coordinate systemsfig, ax = (figsize=(10, 6)) # Draw histogram( data, bins=10, # Number of intervals color="skyblue", # color edgecolor="black", # Border color alpha=0.7, # Transparency density=False # False display frequency, True display frequency) # Add title and tagax.set_title("Numerical distribution histogram", fontsize=14) ax.set_xlabel("Numerical Range", fontsize=12) ax.set_ylabel("Frequency", fontsize=12) (axis="y", linestyle="--") # Show horizontal grid lines# Show chartsplt.tight_layout() ("matplotlib_histogram.png") ()
Core functions and parameters:
-
()
: Histogram drawing function.-
bins
: Number of intervals or specific boundary values (such asbins=[0, 10, 20]
)。 -
color
/edgecolor
: Fill color and border color. -
alpha
: Transparency (0-1). -
density
: Whether to display frequency (normalization).
-
-
ax.set_title()
/ax.set_xlabel()
/ax.set_ylabel()
: Title and axis labels.
3. Use seaborn to draw histograms
seaborn
based onmatplotlib
, providing more concise syntax and statistical functions (such as kernel density estimation).
Code example:
import pandas as pd import seaborn as sns import as plt # Read Excel datadf = pd.read_excel("") data = df["Numerical column"] # Set the themesns.set_theme(style="whitegrid", font="SimHei") # Chinese fonts need to be specified# Create a histogram(figsize=(10, 6)) ax = ( data, bins=10, # Number of intervals kde=True, # Show kernel density curve color="skyblue", # color edgecolor="black", # Border color stat="count" # Statistics type (default count, optional "density")) # Add title and tagax.set_title("Numerical distribution histogram (core density estimation)", fontsize=14) ax.set_xlabel("Numerical Range", fontsize=12) ax.set_ylabel("Frequency", fontsize=12) # Show chartsplt.tight_layout() ("seaborn_histogram.png") ()
Core functions and parameters:
-
()
: Histogram drawing function.-
kde
: Whether to display the kernel density estimation curve (defaultFalse
)。 -
stat
: Statistical type ("count"
Frequency,"density"
Frequency,"percent"
Percentage). -
bins
/color
/edgecolor
: samematplotlib
。
-
-
sns.set_theme()
: Set theme style (such aswhitegrid
、darkgrid
)。
Comparative summary
Library | Features | Applicable scenarios |
---|---|---|
pyecharts |
Need to manually calculate the frequency and generate interactive charts | Requires web page embedding or dynamic interaction |
matplotlib |
Directly drawn, highly customizable | Need to carefully control the details of the chart |
seaborn |
Concise syntax, supporting kernel density estimation | Quickly generate statistical charts |
Things to note
- Data preprocessing: Make sure that the target column in Excel is of a numeric type (non-string).
-
Interval division:Adjustment
bins
Parameter optimization data distribution display (such asbins=20
add details). -
Interactive:
pyecharts
Fit to generate HTML files, andmatplotlib
/seaborn
Suitable for static pictures.
This is the end of this article about Python reading Excel drawing histograms. For more related content about Python reading Excel drawing histograms, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!