echarts散点图(线性回归)

echarts散点图(线性回归)

备注:线性回归是利用数理统计中回归分析,说白了就是根据你的数据算出一个方程式,然后在图中用一条线展示出来

在这里插入图片描述

echarts得版本必须是5.0或以上的

npm install echarts -S
npm install echarts-stat -S
//main.js
import Vue from 'vue'
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts
<div class="charts" ref="chart"></div>

import * as ecStat from 'echarts-stat';
export default {
  data() {
    return {
    };
  },
  mounted() {
    this.getEchart();
  },
  methods: {
    getEchart() {
      this.$echarts.registerTransform(ecStat.transform.regression);
      // 基于准备好的dom,初始化echarts实例
      //light是用来改变展示的颜色
      var myChart = this.$echarts.init(this.$refs.chart, "light");
      var option = {
        dataset: [
          {
            source: [
              [1, 4862.4],
              [2, 5294.7],
              [3, 5934.5],
              [4, 7171.0],
              [5, 8964.4],
              [6, 10202.2],
              [7, 11962.5],
              [8, 14928.3],
              [9, 16909.2],
              [10, 18547.9],
              [11, 21617.8],
              [12, 26638.1],
              [13, 34634.4],
              [14, 46759.4],
              [15, 58478.1],
              [16, 67884.6],
              [17, 74462.6],
              [18, 79395.7],
            ],
          },
          {
            transform: {
              type: "ecStat:regression"
            },
          },
        ],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        xAxis: {
          splitLine: {
            lineStyle: {
              type: "dashed",
            },
          },
        },
        yAxis: {
          splitLine: {
            lineStyle: {
              type: "dashed",
            },
          },
        },
        series: [
          {
            name: "scatter",
            type: "scatter",
            datasetIndex: 0,
          },
          {
            name: "line",
            type: "line",
            smooth: true,
            datasetIndex: 1,
            symbolSize: 0.1,
            symbol: "circle",
            label: { show: true, fontSize: 16 },
            labelLayout: { dx: -20 },
            encode: { label: 2, tooltip: 1 },
          },
        ],
      };
      // 使用指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
  },
};

以上就能显示出散点图了,下面来看看我遇到的问题

报错一

[Vue warn]: Error in mounted hook: “TypeError: data.count is not a function”
TypeError: data.count is not a function

1.这个是因为echarts版本问题(因为我的echarts版本是4.9.0,官方要求是5.0及以上的版本,然后我把版本改成5.1.0就好了)
2.版本改好后还是报这个错,那你就需要 npm install echarts-stat -S

报错二

Error in mounted hook: "TypeError: this. e c h a r t s . r e g i s t e r T r a n s f o r m i s n o t a f u n c t i o n " T y p e E r r o r : t h i s . echarts.registerTransform is not a function" TypeError: this. echarts.registerTransformisnotafunction"TypeError:this.echarts.registerTransform is not a function

这个是因为echarts版本问题(因为我的echarts版本是4.9.0,官方要求是5.0及以上的版本,然后我把版本改成5.1.0就好了)

this.$echarts.registerTransform(ecStat.transform.regression);

报错三

[Vue warn]: Error in mounted hook: “ReferenceError: ecStat is not defined”
ReferenceError: ecStat is not defined

这个是因为没有引入echarts-stat

import * as ecStat from 'echarts-stat';

报错四

[Vue warn]: Error in mounted hook: “TypeError: Cannot read property ‘registerTransform’ of undefined”
TypeError: Cannot read property ‘registerTransform’ of undefined

这个是因为引入echarts的问题(之前没有* as)

import * as echarts from 'echarts';
  • 34
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 35
    评论
要在Python中制作散点图和进行线性回归,你可以使用matplotlib和sklearn库。以下是一种实现方法: 首先,导入所需的库: import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression 然后,准备数据并绘制散点图: x = np.array([2, 5, 8, 8, 13, 15, 17, 19, 21, 24]) y = np.array([12, 31, 45, 52, 79, 85, 115, 119, 135, 145]) plt.scatter(x, y) plt.xlabel("battery") plt.ylabel("KM") plt.title("散点图") plt.show() 接下来,建立线性回归模型并进行拟合: regressor = LinearRegression() regressor.fit(np.reshape(x, (-1, 1)), np.reshape(y, (-1, 1))) 打印拟合结果(参数): print("斜率:", regressor.coef_) print("截距:", regressor.intercept_) 最后,画出数据和拟合直线的图: plt.scatter(x, y) plt.plot(np.reshape(x, (-1, 1)), regressor.predict(np.reshape(x, (-1, 1)))) plt.xlabel("battery") plt.ylabel("KM") plt.title("拟合结果") plt.show() 这样,你就可以在Python中绘制散点图和进行线性回归了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python 编程 线性回归散点图,拟合结果](https://blog.csdn.net/qq_42262610/article/details/124290573)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python机器学习——线性回归案例(一)](https://blog.csdn.net/lucky_shi/article/details/105186572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值