解决方法
方法1:将X轴的样式设置为value
(默认),不要设置为time
(见示例代码)
方法2:设置x轴的最大及最小值
示例代码
import pyecharts.options as opts
from pyecharts.charts import Line
import pandas as pd
import numpy as np
def plot_cumulative_returns(dataframe: pd.DataFrame):
"""绘图:曲线图
:param dataframe: 绘图用的dataframe, 每一行是一个时间点,每一列是一组数据
"""
line_plot = Line(init_opts=opts.InitOpts(width="1400px", height="800px"))
line_plot.add_xaxis(xaxis_data=dataframe.index.strftime("%Y-%m-%d").values.tolist())
for col_name, col_series in dataframe.iteritems():
line_plot.add_yaxis(
series_name=col_name,
y_axis=col_series,
linestyle_opts=opts.LineStyleOpts(width=2, type_='solid'),
label_opts=opts.LabelOpts(is_show=False), # 不显示坐标值
)
# 设置全局样式
line_plot.set_global_opts(
xaxis_opts=opts.AxisOpts(name="时间", ), # 设置横坐标代表的含义
datazoom_opts=opts.DataZoomOpts(
is_show=True,
range_start=50,
),
)
line_plot.render("my_plot.html")
def main():
data = np.random.random(size=(200, 2))
data = np.cumsum(data, axis=0)
plot_df = pd.DataFrame(data,
index=pd.date_range(start="20200101", periods=200, freq="D"),
columns=["line1", "line2"])
plot_cumulative_returns(plot_df)
if __name__ == '__main__':
main()