使用python在2D圆上绘制垂直向量

时间:2018-03-13 22:04:47

标签: python matplotlib

在我的代码中,我假设2D圆是一个类似于地球但是蓝色的行星。我想在蓝色圆圈的边缘绘制两个矢量a_h和a_r彼此垂直,但我不确定如何将垂直矢量a_h和a_r放在圆的顶部或底部。ExpertsExchange What I want to plot in python not circular?

这是我正在使用的代码:

// REQUIRE REQUIRED FRAMEWORKS AND LIBS
var express   = require("express");
var mongoose  = require("mongoose");
var bParser   = require("body-parser");
var ejs       = require("ejs");
var mOverride = require("method-override");


//INITIALIZE EXPRESS, SETUP DB AND CALL LIBS
var app = express();
mongoose.connect("mongodb://localhost/exerciseApp");
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bParser.urlencoded({extended: true}));
app.use(mOverride("_method"));

//DB TEMPLATE
var exerciseSchema = new mongoose.Schema({
    user: String,
    exercise: [String],
    previous: [Number],
    latest: [Number]
});

var Exe = mongoose.model("Exe", exerciseSchema);

//**********************TESTING PURPOSES******************************

//CREATE TEST DATA
// Exe.create({
//     user: "Template", 
//     exercise: ["Test"],
//     previous: [0],
//     latest: [0]
// });

// Exe.create({
//     user: "Nathan", 
//     exercise: ["Bench Press"],
//     previous: [35],
//     latest: []
// });

// Exe.findOne({name: "Marc"}, function(err, test){
//     console.log(test);
// });

//****************************************************

//ROUTES

app.get("/", function(req,res){
   res.redirect("/index"); 
});

app.get("/index", function(req,res){
    Exe.find({}, function(err, exercise){
        if (err){
            res.render("Oops, Somthing went wrong!");
        }else{
            res.render("index", {exes: exercise});
        }
    });
});

app.get("/index/new", function(req,res){
   res.render("new"); 
});

app.put("/index", function(req,res){
    Exe.update({}, {$push: req.body.exe}, {multi: true},  function(err, numAffected){ //ADDS NEW WORKOUT TO EACH USER
        if (err){
            console.log(err);
        }else{
            Exe.update({}, {$push: {"previous" : 0}}, {multi: true},  function(err, numAffected){ //PUSH STARTING VALUE OF 0 INTO PREVIOUS WEIGHT.
                if (err){
                    console.log(err);
                }else{
                    res.redirect("/index");
                }   
            });
        }
    });
});


app.put("/index/new_weight", function(req,res){
    Exe.findById(req.body.exercise_id, function(err, array){
        if (err){
            console.log(err);
            res.send(err);
        }else{
            var value = [0];
            var oldArray = array.previous;
            var newArray = req.body.exe.previous;
            for (var i = 0; i < newArray.length; i++){
                if (newArray[i] === ""){
                    value.push(oldArray[i]);
                }else{
                    value.push(newArray[i]);
                }
            Exe.findByIdAndUpdate(req.body.exercise_id, {$set : {"previous" : value}}, function(err, newArray){
                if (err){
                    console.log(err);
                }else{
                    res.redirect("/index");
                }
            });     
        }
    });
});


app.get("/index/new/user", function(req,res){
    res.render("newuser");
});

app.post("/index/new/user", function(req,res){
    Exe.findOne({},{},{sort: {create_at: -1}}).lean().exec(function (err, templateSchema){ //.LEAN AND EXEC RETURNS COLLECTION MODEL AS A JS OBJECT.
       if (err){
           console.log(err);
           res.send(err);
       } else{
           delete templateSchema["_id"];
           templateSchema.user = req.body.exe.user;
           Exe.create(templateSchema);
           res.redirect("/index");
       }
    });
});



// SETUP SERVER AND LISTEN ON PORT.  
app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Excercise App Running...");
});

任何帮助都将非常感谢!!

2 个答案:

答案 0 :(得分:0)

Circle命令在您的代码中不执行任何操作。为什么不使用scatter圆形标记?

plt.scatter(0., 0., 100, color='blue')

答案 1 :(得分:0)

这里不使用散点图是有意义的,因为这需要计算散布在数据单元中的点数。相反,使用圆圈很好。

箭头位于根据月球轨道R_avg的位置。它们的长度在这里是任意的,但当然你可以把它改成你喜欢的任何东西。

import matplotlib.pyplot as plt

R_min=356.5e6 #Perigee
R_max=406.7e6 #Apogee
R_avg = 0.5*(R_min + R_max)
r = 6.3781e6 #radius of Earth

ax = plt.gca()
ax.set_aspect("equal")
i = 0

# create arrows on moon orbit, both one orbit radius in length to both directions
ax.quiver([0,0], [R_avg,R_avg], [1,0], [0, 1], angles='xy',         
          scale_units='xy', scale=1./R_avg)

# create earth and moon orbit
earth = plt.Circle((0,0),r)
orbit = plt.Circle((0,0),R_avg, fill=False, color="crimson")
ax.add_patch(earth)
ax.add_patch(orbit)
ax.autoscale_view()
ax.margins(1)

plt.show()

enter image description here