SoFunction
Updated on 2024-12-10

python pyecharts implements a file to plot multiple charts

Grid displays multiple charts in parallel

Note: The first plot needs to be a plot with x/y axes, i.e. it cannot be a Pie, the other positions are in any order.

from pyecharts import Bar, Line, Scatter, EffectScatter, Grid
'''
 Grid class: display multiple charts in parallel TODO The first chart needs to be a chart with x/y axis, i.e. it can't be a Pie, and the other positions are in any order.
'''

attr = ["Shirt.", "Wool sweater.", "Chiffon shirt.", "Pants.", "High heels.", "Socks."]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("Example of a bar chart", title_pos="65%")
("Merchant A", attr, v1, is_stack=True)
("Merchant B", attr, v2, is_stack=True, legend_pos="80%")

line = Line("Example of a line graph")
attr = ["Monday.", "Tuesday.", "Wednesday.", "Thursday.", "Friday.", "Saturday.", "Sunday."]
(
 "Maximum temperature",
 attr,
 [11, 11, 15, 13, 12, 13, 10],
 mark_point=["max", "min"],
 mark_line=["average"],
)
(
 "Minimum temperature",
 attr,
 [1, -2, 2, 5, 3, 2, 0],
 mark_point=["max", "min"],
 mark_line=["average"],
 legend_pos="20%",
)

v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
scatter = Scatter("Example of a scatterplot", title_top="50%", title_pos="65%")
("scatter", v1, v2, legend_top="50%", legend_pos="80%")

es = EffectScatter("Example of a dynamic scatterplot", title_top="50%")
(
 "es",
 [11, 11, 15, 13, 12, 13, 10],
 [1, -2, 2, 5, 3, 2, 0],
 effect_scale=6,
 legend_top="50%",
 legend_pos="20%",
)

# TODO Configuring the Grid Class
grid = Grid(height=720, width=1200) # Initialization, parameters can pass page_title,width,height.
(bar, grid_bottom="60%", grid_left="60%") # Add the chart to be displayed and set the display position
(line, grid_bottom="60%", grid_right="60%") # Add the chart to be displayed and set the display position
(scatter, grid_top="60%", grid_left="60%") # Add the chart to be displayed and set the display position
(es, grid_top="60%", grid_right="60%") # Add the chart to be displayed and set the display position

(". /pyecharts_html/Grid_Parallel_Display_Multiple_Charts.html")

Overlap chart mixing

from pyecharts import Line, Bar, Overlap
'''
 Multiple x y axis examples
'''

attr = ["{}moon".format(i) for i in range(1, 13)]
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]

bar = Bar()
("Evaporation", attr, v1)
("Precipitation", attr, v2, yaxis_formatter=" ml",
 yaxis_interval=50, yaxis_max=250)

line = Line()
("Average temperature", attr, v3, yaxis_formatter=" °C", yaxis_interval=5)

overlap = Overlap(width=1200, height=600) # Instantiation
# No x and y axes are added by default, and the x and y axes are indexed 0
(bar)
# Add a new y-axis, when the number of y-axes is 2 and the second y-axis has an index of 1 (index starts at 0), so set yaxis_index = 1
# Since the same x-axis is used, the x-axis part does not need to be changed
(line, yaxis_index=1, is_add_yaxis=True)

(". /pyecharts_html/Overlap_Chart_Mix.html")

Page renders multiple charts sequentially on a single page

from pyecharts import *

page = Page() # Instantiate the page class

# line
attr = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
line = Line("Example of a line graph")
("Maximum temperature", attr, [11, 11, 15, 13, 12, 13, 10],
  mark_point=["max", "min"], mark_line=["average"])
("Minimum temperature", attr, [1, -2, 2, 5, 3, 2, 0],
  mark_point=["max", "min"], mark_line=["average"])

(line) # TODO Adding charts to a page

# pie
attr = ["Shirt.", "Wool sweater.", "Chiffon shirt.", "Pants.", "High heels.", "Socks."]
v1 = [11, 12, 13, 10, 10, 10]
pie = Pie("Pie Chart - Circle Chart Example", title_pos='center')
("", attr, v1, radius=[40, 75], label_text_color=None,
 is_label_show=True, legend_orient='vertical', legend_pos='left')

(pie) # TODO Adding charts to a page

# kline
v1 = [[2320.26, 2320.26, 2287.3, 2362.94],
 [2300, 2291.3, 2288.26, 2308.38],
 [2295.35, 2346.5, 2295.35, 2345.92],
 [2347.22, 2358.98, 2337.35, 2363.8],
 [2360.75, 2382.48, 2347.89, 2383.76],
 [2383.43, 2385.42, 2371.23, 2391.82],
 [2377.41, 2419.02, 2369.57, 2421.15],
 [2425.92, 2428.15, 2417.58, 2440.38],
 [2411, 2433.13, 2403.3, 2437.42],
 [2432.68, 2334.48, 2427.7, 2441.73],
 [2430.69, 2418.53, 2394.22, 2433.89],
 [2416.62, 2432.4, 2414.4, 2443.03],
 [2441.91, 2421.56, 2418.43, 2444.8],
 [2420.26, 2382.91, 2373.53, 2427.07],
 [2383.49, 2397.18, 2370.61, 2397.94],
 [2378.82, 2325.95, 2309.17, 2378.82],
 [2322.94, 2314.16, 2308.76, 2330.88],
 [2320.62, 2325.82, 2315.01, 2338.78],
 [2313.74, 2293.34, 2289.89, 2340.71],
 [2297.77, 2313.22, 2292.03, 2324.63],
 [2322.32, 2365.59, 2308.92, 2366.16],
 [2364.54, 2359.51, 2330.86, 2369.65],
 [2332.08, 2273.4, 2259.25, 2333.54],
 [2274.81, 2326.31, 2270.1, 2328.14],
 [2333.61, 2347.18, 2321.6, 2351.44],
 [2340.44, 2324.29, 2304.27, 2352.02],
 [2326.42, 2318.61, 2314.59, 2333.67],
 [2314.68, 2310.59, 2296.58, 2320.96],
 [2309.16, 2286.6, 2264.83, 2333.29],
 [2282.17, 2263.97, 2253.25, 2286.33],
 [2255.77, 2270.28, 2253.31, 2276.22]]
kline = Kline("Example of a K-chart.")
("Day K.", ["2017/7/{}".format(i + 1) for i in range(31)], v1)

(kline) # TODO Adding charts to a page

# radar
schema = [
 ("Selling.", 6500), ("Management", 16000), ("Information technology", 30000),
 ("Customer Service", 38000), ("Research and Development", 52000), ("The market.", 25000)
]
v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
radar = Radar("Example of a radar chart")
(schema)
("Budget allocation", v1, is_splitline=True, is_axisline_show=True)
("Actual expenses", v2, label_color=["#4e79a7"], is_area_show=False,
  legend_selectedmode='single')

(radar) # TODO Adding charts to a page
(". /pyecharts_html/Page_Rendering_multiple_charts_on_one_page.html")

Timeline:Timeline rotating multi-chart

from pyecharts import Bar, Line, Timeline, Overlap
from random import randint

attr = ["{}moon".format(i) for i in range(1, 7)]
bar = Bar("January data", "The data is pure fiction.")
("bar", attr, [randint(10, 50) for _ in range(6)])
line = Line()
("line", attr, [randint(50, 80) for _ in range(6)])
overlap = Overlap()
(bar)
(line)

bar_1 = Bar("February data", "The data is pure fiction.")
bar_1.add("bar", attr, [randint(10, 50) for _ in range(6)])
line_1 = Line()
line_1.add("line", attr, [randint(50, 80) for _ in range(6)])
overlap_1 = Overlap()
overlap_1.add(bar_1)
overlap_1.add(line_1)

bar_2 = Bar("March data", "The data is pure fiction.")
bar_2.add("bar", attr, [randint(10, 50) for _ in range(6)])
line_2 = Line()
line_2.add("line", attr, [randint(50, 80) for _ in range(6)])
overlap_2 = Overlap()
overlap_2.add(bar_2)
overlap_2.add(line_2)

bar_3 = Bar("April data", "The data is pure fiction.")
bar_3.add("bar", attr, [randint(10, 50) for _ in range(6)])
line_3 = Line()
line_3.add("line", attr, [randint(50, 80) for _ in range(6)])
overlap_3 = Overlap()
overlap_3.add(bar_3)
overlap_3.add(line_3)

bar_4 = Bar("May data", "The data is pure fiction.")
bar_4.add("bar", attr, [randint(10, 50) for _ in range(6)])
line_4 = Line()
line_4.add("line", attr, [randint(50, 80) for _ in range(6)])
overlap_4 = Overlap()
overlap_4.add(bar_4)
overlap_4.add(line_4)

timeline = Timeline(
   page_title = "Page tab name",
   width=600,
   height=600,
   is_auto_play= True, # whether to autoplay, default=False
   is_loop_play= True, # Whether to loop or not
   is_rewind_play=False, # Reverse playback
   is_timeline_show=True, # Whether to display the timeline, default=true
   timeline_play_interval=1000, # Playback interval, ms
   timeline_symbol= "arrow", # point-in-time marker graphics, 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow'
   timeline_symbol_size= [15,8], # Graphic size, can be a number and a list, the list indicates width and height
   timeline_left= "1% ", # Distance to the left , timeline_right
   timeline_bottom=0, # timeline_top
  )
(overlap, 'January')
(overlap_1, 'February')
(overlap_2, 'March')
(overlap_3, 'April')
(overlap_4, 'May')
(". /pyecharts_html/Timeline_Timeline_Rotating_Multiple_Charts.html")

Above this python pyecharts to realize a file to draw multiple charts is all I have to share with you, I hope to give you a reference, and I hope you support me more.