Adding a coordinate point marker in sympy Python

Question:

I want to put a marker on the numbers 1, 2, 3, 4 , 5, 6 which I repeat from numpy arrange. Here, and here it is already there for learning materials, but it doesn’t work as well as I want.

Can anyone help me with this problem, any kind of help will be highly appreciated.

This is my code

import numpy as np
from sympy import symbols, plot
from sympy.plotting import plot 
from sympy import *
x = symbols("x")
init_printing(use_unicode=True)
ekpr =  x**2
a = np.arange(1, 7, 1)
for sol in a:
   if sol <= 6:
       print(sol)

plot(
    ekpr.rewrite(Add), xlim=[0, 8],
        markers=[{
            "args": [sol, [0], a], # coordinates of the point
            "marker": "o" # the type of marker to use
       }]

)

The result is like this picture.

Why is it that only one coordinate point is all blue? And I expect the coordinates to be all blue following the number 6

Asked By: Fahrizal

||

Answers:

Matplotlib requires coordinates, namely x-component and y-component.

When you call the plot function and create markers, you need to tell it what are your coordinates. Let’s see what you did:

"args": [sol, [0], a]

Here, you told matplotlib to place a marker at coordinate (6, 0), but then matplotlib loop over a: let ai be the ith element of a, Matplotlib will place a marker at the coordinate (ai, ai). Hence, you can see in your picture the orange line.

Instead, you need to provide a as the x-coordinate and 0 as the y-coordinate. But, since a is an iterable (array, or list, or tuple, …), then y-coordinate must also be an iterable. So, [0] * len(a) is going to create a list of zeros with as many elements as a.

plot(
    ekpr.rewrite(Add), xlim=[0, 8],
        markers=[{
            "args": [a, [0] * len(a)], # coordinates of the point
            "marker": "o", # the type of marker to use
            "linestyle": "none"
       }]
)
Answered By: Davide_sd
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.