1

I am new to python and I need to do a class made of different functions. Each function should be plotting different type of plot with predefined color, size, etc. The user should only call the function and enter two values (x=df or lst, y=df or lst and z the title of the plot) and then get the plot with the format that I have already predefined in the code.

Here is my code:

    class Plotter:
    def plot_signal(x,y):
        plt.figure(figsize=(20, 5) )
        plt.plot(x,
                 y,
                 color=(142/255.0,186/255.0,229/255.0), alpha=0.1 , linewidth=0.5 );
        plt.xlim(left=0)
        plt.xlabel('Time [s]', fontsize=11,  fontname='Arial')
        plt.ylabel('Force [kN]', fontsize=11, fontname='Arial')
        plt.xticks(fontsize=8)
        plt.yticks(fontsize=8)
        plt.savefig(r"filepath\\filename.jpg")
        plt.show()
        plt.title(z)
        return plt.show()

z = "trial"
x = np.random.rand(100)*10
y = np.random.rand(100)

plot_signal(x,y)

and the result (as seen in the photo), is a plot with a different color other than the color I chose, no title, and not even the figure size, it does also save my file, and I do not know how to fix it or what is the problem.

enter image description here

4
  • I know I have indent problem in the posted code but that is actually a mistake happened when I was trying to post my question Commented Apr 17, 2020 at 2:32
  • 1
    Please fix the indentation then, you can edit your question. Commented Apr 17, 2020 at 3:18
  • I do not know how to edit my question after posting it. Commented Apr 18, 2020 at 13:40
  • You can find the "edit" button just underneath your question and above the comments section. Commented Apr 18, 2020 at 15:06

1 Answer 1

1
def plot_signal(x, y, z):
    plt.figure(figsize=(20, 5))
    plt.plot(x, y, color=(142/255.0,186/255.0,229/255.0))
    plt.xlabel('Time [s]', fontsize=11,  fontname='Arial')
    plt.ylabel('Force [kN]', fontsize=11, fontname='Arial')
    plt.xticks(fontsize=8)
    plt.title(z)
    plt.yticks(fontsize=8)
    plt.show()

you can change the color by just using color string, the list of the color string can be seen here

example

plt.plot(x, y, color='red')

based on your code, you use alpha=0.1 which means the color will be transparent if it close to 0.

plt.savefig(r"filepath\\filename.jpg")

this line will save your file, you can remove it

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. In this case it is not automated. What I need to do, is save this as function and people can call the function and enter (x=,y=, z=) and get the plot plotted and saved I also do not want them to change the color

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.