mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-09 20:41:14 +00:00
test: use pyecharts to replace matplotlib in iperf test
This commit is contained in:
@@ -12,18 +12,13 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import matplotlib
|
||||
# fix can't draw figure with docker
|
||||
matplotlib.use('Agg')
|
||||
import matplotlib.pyplot as plt # noqa: E402 - matplotlib.use('Agg') need to be before this
|
||||
from collections import OrderedDict
|
||||
|
||||
import pyecharts.options as opts
|
||||
from pyecharts.charts import Line
|
||||
|
||||
|
||||
# candidate colors
|
||||
LINE_STYLE_CANDIDATE = ['b-o', 'r-o', 'k-o', 'm-o', 'c-o', 'g-o', 'y-o',
|
||||
'b-s', 'r-s', 'k-s', 'm-s', 'c-s', 'g-s', 'y-s']
|
||||
|
||||
|
||||
def draw_line_chart(file_name, title, x_label, y_label, data_list):
|
||||
def draw_line_chart(file_name, title, x_label, y_label, data_series, range_list):
|
||||
"""
|
||||
draw line chart and save to file.
|
||||
|
||||
@@ -31,20 +26,33 @@ def draw_line_chart(file_name, title, x_label, y_label, data_list):
|
||||
:param title: chart title
|
||||
:param x_label: x-axis label
|
||||
:param y_label: y-axis label
|
||||
:param data_list: a list of line data.
|
||||
each line is a dict of ("x-axis": list, "y-axis": list, "label": string)
|
||||
:param data_series: a dict {"name": data}. data is a dict.
|
||||
:param range_list: a list of x-axis range
|
||||
"""
|
||||
|
||||
plt.figure(figsize=(12, 6))
|
||||
plt.grid(True)
|
||||
|
||||
for i, data in enumerate(data_list):
|
||||
plt.plot(data["x-axis"], data["y-axis"], LINE_STYLE_CANDIDATE[i], label=data["label"])
|
||||
|
||||
plt.xlabel(x_label)
|
||||
plt.ylabel(y_label)
|
||||
plt.legend(fontsize=12)
|
||||
plt.title(title)
|
||||
plt.tight_layout(pad=3, w_pad=3, h_pad=3)
|
||||
plt.savefig(file_name)
|
||||
plt.close()
|
||||
line = Line()
|
||||
# echarts do not support minus number for x axis, convert to string
|
||||
_range_list = [str(x) for x in range_list]
|
||||
line.add_xaxis(_range_list)
|
||||
for item in data_series:
|
||||
_data = OrderedDict.fromkeys(_range_list, None)
|
||||
for key in data_series[item]:
|
||||
_data[str(key)] = data_series[item][key]
|
||||
_data = list(_data.values())
|
||||
try:
|
||||
legend = item + " (max: {:.02f})".format(max([x for x in _data if x]))
|
||||
except TypeError:
|
||||
legend = item
|
||||
line.add_yaxis(legend, _data, is_smooth=True, is_connect_nones=True,
|
||||
label_opts=opts.LabelOpts(is_show=False))
|
||||
line.set_global_opts(
|
||||
datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=100),
|
||||
title_opts=opts.TitleOpts(title=title, pos_left="center"),
|
||||
legend_opts=opts.LegendOpts(pos_top="10%", pos_left="right", orient="vertical"),
|
||||
tooltip_opts=opts.TooltipOpts(trigger="axis"),
|
||||
xaxis_opts=opts.AxisOpts(type_="category", name=x_label, splitline_opts=opts.SplitLineOpts(is_show=True)),
|
||||
yaxis_opts=opts.AxisOpts(type_="value", name=y_label,
|
||||
axistick_opts=opts.AxisTickOpts(is_show=True),
|
||||
splitline_opts=opts.SplitLineOpts(is_show=True)),
|
||||
)
|
||||
line.render(file_name)
|
||||
|
Reference in New Issue
Block a user