Python Forum

Full Version: Error when plotting a graph.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been trying to make a python program that will get the values : m, x, c. And plot a graph using the equation y = mx+c.
Im also adding a gui to this.
I'm using matplotlib, tkinter and numpy to achieve this.
Code snippet:
from matplotlib import pyplot as plt
import numpy as np
from tkinter import *

def plot():
    try:
        m_val = int(Em.get()) #Em stands for entry m. Tkinter stuff
        x_val = int(Ex.get()) #x_val stands for x value & is used in calculations
        c_val = int(Ec.get())
    except TypeError as err:
        #error(err)
        pass

    _x = np.linspace(-5,5,100)
    _y = m_val*x_val+c_val
    plt.plot(_x, _y, '-r', label=f'y={m_val}*{x_val}+{c_val}')
    plt.title('Graph of y=2x+1')
    plt.xlabel('x', color='#1C2833')
    plt.ylabel('y', color='#1C2833')
    plt.legend(loc='upper left')
    plt.grid()
    plt.show()
I get this:
Error:
raise ValueError(f"x and y must have same first dimension, but " ValueError: x and y must have same first dimension, but have shapes (100,) and (1,)
Traceback:
Error:
line 39, in plot plt.plot(_x, _y, '-r', label=f'y={m_val}*{x_val}+{c_val}')
I have used matplotlib before, and when x & y values are not equal, I seem to get the same error.
Is this a defect in matplotlib or am I using it wrong.

Thanks for spending your time to help!
~Oshadha
_x is a numpy array. _y is a single number. Do you want to evaluate m_val*x_val+c_val for each value in _x?
import numpy as np
import matplotlib.pyplot as plt

slope = 2
offset = 3
x = np.linspace(-5,5,100)
y = x * slope + offset
plt.plot(x, y)
plt.show()
Another thing. This makes no sense:
x_val = int(Ex.get()) #x_val stands for x value & is used in calculations
x_val is a range, not a single value. You should have the user enter the range for x, not a single value.
(Mar-14-2022, 04:06 PM)deanhystad Wrote: [ -> ]_x is a numpy array. _y is a single number. Do you want to evaluate m_val*x_val+c_val for each value in _x?
import numpy as np
import matplotlib.pyplot as plt

slope = 2
offset = 3
x = np.linspace(-5,5,100)
y = x * slope + offset
plt.plot(x, y)
plt.show()

Well no.
I want it to be readable and accurate enough, any ideas?
I do not understand your response. "Well no"? Well no to what? You don't want to plot y= mx+c? This is how you plot y = mx+c
slope = 2  # m
offset = 3  # c
x = np.linspace(-5,5,100)  # Range of x values for solving y
y = x * slope + offset   # y = mx + c
And what do you mean by "accurate enough"? I think values will be calculated to 64 bit resolution. If you want higher resolution in the plot specify more values for the x axis. Can you please provide more information about what you mean by "accurate enough".

If you don't like how the plot is labeled, I intentionally left out labels and legend and the like to focus on the main problem. Now that you should know how to get y values to plot you can do any formatting that you want.