Pyecharts is a Python data visualization library based on ECharts, allowing users to generate various types of interactive charts and data visualizations using Python.
ECharts is a powerful open-source data visualization library developed by Baidu, and Pyecharts is a Python wrapper for ECharts, making it easier to use ECharts within Python.
Pyecharts provides a simple yet flexible API that enables users to easily create a variety of charts, including but not limited to line charts, bar charts, scatter plots, pie charts, maps, and more.
With Pyecharts, users can process and prepare data using Python and then generate interactive charts with concise code. These charts can be embedded in web applications or saved as static files.
Features and Functionality of Pyecharts
Easy to Use: Pyecharts offers an intuitive and user-friendly API, allowing users to quickly get started and easily generate various charts.
Rich Chart Types: Supports a wide range of common chart types, including line charts, bar charts, scatter plots, pie charts, and maps, meeting different scenario requirements.
Support for Mainstream Data Formats: Can handle common data formats such as lists, dictionaries, and Pandas DataFrames.
Interactivity: Generated charts can be interactive, allowing users to interact with them through mouse hover, zooming, and more.
Rich Configuration Options: Provides a wealth of configuration options, allowing users to customize chart styles, layouts, and other properties.
Theme Support: Offers various themes, enabling users to choose a suitable theme that aligns the chart with the overall style of the application.
Installation of Pyecharts
Installation via pip:
pip install pyecharts
Source Code Installation:
$ git clone https://github.com/pyecharts/pyecharts.git $ cd pyecharts $ pip install -r requirements.txt $ python setup.py install # Alternatively, execute python install.py
After successful installation, you can check the version of Pyecharts:
import pyecharts print(pyecharts.__version__)
Output:
2.0.4
Pyecharts Chart Types
Pyecharts supports the following chart types:
Chart Type | Pyecharts Class | Package Import |
---|---|---|
Line Chart | Line | from pyecharts.charts import Line |
Bar Chart | Bar | from pyecharts.charts import Bar |
Scatter Plot | Scatter | from pyecharts.charts import Scatter |
Pie Chart | Pie | from pyecharts.charts import Pie |
Radar Chart | Radar | from pyecharts.charts import Radar |
Heatmap | HeatMap | from pyecharts.charts import HeatMap |
K Line Chart | Kline | from pyecharts.charts import Kline |
Boxplot | Boxplot | from pyecharts.charts import Boxplot |
Map | Map | from pyecharts.charts import Map |
Word Cloud | WordCloud | from pyecharts.charts import WordCloud |
Gauge | Gauge | from pyecharts.charts import Gauge |
Funnel Chart | Funnel | from pyecharts.charts import Funnel |
Tree Diagram | Tree | from pyecharts.charts import Tree |
Parallel Coordinates | Parallel | from pyecharts.charts import Parallel |
Sankey Diagram | Sankey | from pyecharts.charts import Sankey |
Geo Map | Geo | from pyecharts.charts import Geo |
Timeline Chart | Timeline | from pyecharts.charts import Timeline |
3D Scatter Plot | Scatter3D | from pyecharts.charts import Scatter3D |
3D Bar Chart | Bar3D | from pyecharts.charts import Bar3D |
3D Surface Plot | Surface3D | from pyecharts.charts import Surface3D |
In this tutorial, we will use Pyecharts to create a simple bar chart that displays the sales data for five months:
Example
from pyecharts.charts import Bar # Prepare data x_data = ['January', 'February', 'March', 'April', 'May'] y_data = [10, 20, 15, 25, 30] # Create bar chart bar_chart = Bar() bar_chart.add_xaxis(x_data) bar_chart.add_yaxis("Sales", y_data) # You can also pass a file path, like bar_chart.render("bar_chart.html") bar_chart.render()
If no file path is specified in the bar_chart.render()
method, Pyecharts will generate a file named "render.html"
in the current working directory, containing the chart.
File Output:
The result of the above code will generate a chart saved in "render.html"
.
If you wish to standardize the file name or specify the path for saving the chart, you can pass the file path as an argument in the render()
method, for example:
bar_chart.render("my_bar_chart.html")
This will save the generated chart as "my_bar_chart.html"
in the current working directory.
Setting Chart Configuration Options
In the previous example, the chart title was "Monthly Sales Bar Chart," with months on the horizontal axis and sales on the vertical axis. You can adjust the data and chart configuration according to your needs:
Example
from pyecharts import options as opts from pyecharts.charts import Bar # Prepare data x_data = ['January', 'February', 'March', 'April', 'May'] y_data = [10, 20, 15, 25, 30] # Create bar chart bar_chart = Bar() bar_chart.add_xaxis(x_data) bar_chart.add_yaxis("Sales", y_data) # Configure chart options bar_chart.set_global_opts( title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart"), xaxis_opts=opts.AxisOpts(name="Month"), yaxis_opts=opts.AxisOpts(name="Sales (in ten thousand yuan)"), ) # Render chart bar_chart.render("bar_chart.html")
Explanation:
Bar(): Creates a bar chart object.
add_xaxis and add_yaxis: Used to add data to the horizontal and vertical axes, respectively.
set_global_opts: Configures global options, including the title, axis names, etc.
The generated chart will be saved as "bar_chart.html"
, which you can open in your browser to view the bar chart.
Using Themes
Pyecharts supports theme switching, allowing users to change the chart's appearance according to their needs.
Pyecharts provides over 10 built-in themes, and developers can also customize their own themes.
Below is a simple example demonstrating how to switch themes in Pyecharts:
Example
from pyecharts import options as opts from pyecharts.charts import Bar # Built-in themes can be viewed at pyecharts.globals.ThemeType from pyecharts.globals import ThemeType # Prepare data x_data = ['January', 'February', 'March', 'April', 'May'] y_data = [10, 20, 15, 25, 30] # Create bar chart with initial theme set to LIGHT bar_chart = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) bar_chart.add_xaxis(x_data) bar_chart.add_yaxis("Sales", y_data) # Configure chart options bar_chart.set_global_opts( title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart"), xaxis_opts=opts.AxisOpts(name="Month"), yaxis_opts=opts.AxisOpts(name="Sales (in ten thousand yuan)"), ) # Switch to DARK theme bar_chart.set_global_opts(theme=ThemeType.DARK) # Render chart bar_chart.render("themed_bar_chart.html")
Explanation:
This example shows how to use ThemeType to switch themes in Pyecharts. Supported themes include LIGHT and DARK, and you can choose the appropriate theme according to your needs.
init_opts=opts.InitOpts(theme=ThemeType.LIGHT)
: Specifies the initial theme as LIGHT when creating the chart object.
Supported Themes in Pyecharts
Light Themes:
"LIGHT": Default light theme
"WESTEROS": Warm color theme
"CHALK": Chalkboard-style theme
"ESSOS": Soft green theme
"INFOGRAPHIC": Infographic-style theme
"MACARONS": Sweet candy-colored theme
Dark Themes:
"DARK": Default dark theme
"PURPLE-PASSION": Deep purple theme
"SHINE": Simple black theme
"VINTAGE": Vintage-style theme
"ROMA": Ancient Roman-style theme
"WALDEN": Forest dark theme
Users can also create custom themes by defining custom colors and styles.、
Setting Global Configuration Options in Pyecharts
In Pyecharts, the set_global_opts
method is used to set global configuration options. This method allows you to configure various global properties of the chart, such as the title, axes, legend, etc.
Below are some commonly used global configuration options:
bar_chart.set_global_opts( title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart", subtitle="Subtitle"), xaxis_opts=opts.AxisOpts(name="Month"), yaxis_opts=opts.AxisOpts(name="Sales (in ten thousand yuan)"), legend_opts=opts.LegendOpts(pos_left="center", pos_top="top"), toolbox_opts=opts.ToolboxOpts(), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), )
Explanation:
title_opts: Configures the chart title. You can set the main title, subtitle, and related styles.
xaxis_opts and yaxis_opts: These options configure the x-axis and y-axis. You can set the axis name, axis line style, etc.
legend_opts: Configures the legend, allowing you to set its position and style.
toolbox_opts: Configures the toolbox, which provides interactive tools like saving the chart as an image or viewing the data.
tooltip_opts: Configures the tooltip, including the trigger method and style.
Example
from pyecharts import options as opts from pyecharts.charts import Bar # Prepare data x_data = ['January', 'February', 'March', 'April', 'May'] y_data = [10, 20, 15, 25, 30] # Create bar chart bar_chart = Bar() bar_chart.add_xaxis(x_data) bar_chart.add_yaxis("Sales", y_data) # Configure global properties bar_chart.set_global_opts( title_opts=opts.TitleOpts(title="Monthly Sales Bar Chart", subtitle="Subtitle"), xaxis_opts=opts.AxisOpts(name="Month"), yaxis_opts=opts.AxisOpts(name="Sales (in ten thousand yuan)"), legend_opts=opts.LegendOpts(pos_left="center", pos_top="top"), toolbox_opts=opts.ToolboxOpts(), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), ) # Render chart bar_chart.render("global_options_bar_chart.html")
The generated chart will be saved as "global_options_bar_chart.html"
. You can open this file in your browser to view the bar chart with the configured global options.