Python Forum
Error when plotting a graph.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when plotting a graph.
#1
Exclamation 
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
Reply
#2
_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.
Reply
#3
(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?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting by Time, error mansoorahs 1 739 May-16-2023, 09:46 AM
Last Post: Larz60+
  Plotting simple graph (AttributeError) Laplace12 0 1,377 Jul-28-2021, 10:58 AM
Last Post: Laplace12
  Error When Plotting ValueError: x and y must have same first dimension JoeDainton123 1 9,114 Oct-04-2020, 12:58 PM
Last Post: scidam
  Graph not plotting Hass 1 1,961 Sep-14-2019, 09:52 AM
Last Post: luoheng
  Plotting number on graph william888 1 1,722 Sep-02-2019, 02:36 AM
Last Post: scidam
  Graph Plotting Help Talch 1 2,237 Aug-16-2018, 10:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020