Skip to main content

Command Palette

Search for a command to run...

MatplotLib - Customizing graphs

Updated
•2 min read•View as Markdown
V

Greetings! I'm Vishnu Vinay, a Computer Science and Engineering graduate holding a B. Tech degree. Currently immersed in the captivating world of Artificial Intelligence, I am on a quest for knowledge while pursuing a graduate certificate in Artificial Intelligence with Machine Learning. My passion lies in sharing insights and discoveries in the fields of AI, Machine Learning, Artificial General Intelligence, and Robotics through engaging blog posts. Proficient in Python, ML libraries, and algorithms, I find joy in developing and deploying ML models, with a focus on leveraging AWS Sagemaker. Join me on this exciting journey of unraveling the mysteries of AI through the lens of coding, exploration, and the ever-evolving landscape of machine learning. Let's embark on this knowledge-sharing adventure together!

1) change the color of the line

x = [1, 2, 3]

y = [3, 4, 5]

/ run either one of the below lines to see the changes

plt.plot(x, y, "b") -> blue color line

plt.plot(x, y, "r") -> red color line

plt.plot(x, y, color = "black") -> another option

plt.show()

2) changing the pattern of the line

x = [1, 2, 3]

y = [3, 4, 5]

/ run either one of the below lines to see the changes

/ Here, the 1st letter is for color and the 2nd letter and onwards will be used for pattern types.

plt.plot(x, y, "ro") -> dot-like line

plt.plot(x, y, "r--") -> dashed line

plt.plot(x, y, color = "black", marker = "--") -> another option

plt.show()

Note: Check the documentation for more color options and pattern types

3) changing the line width

x = [1, 2, 3]

y = [3, 4, 5]

/ run either one of the below lines to see the output

plt.plot(x, y, color = "black", linewidth = 0.5) -> thin line

plt.plot(x, y, color = "black", linewidth = 5) -> thicker line

plt.show()

4) adding x and y label

x = [1, 2, 3]

y = [3, 4, 5]

plt.xlabel("x")

plt.ylabel("y")

plt.plot(x, y, color = "black")

plt.show()

Note: Make sure the values passed in xlabel() and ylabel() be inside " "

5) adding the title

x = [1, 2, 3]

y = [3, 4, 5]

plt.title("Demo")

plt.plot(x, y, color = "black")

plt.show()

6) adding names to lines and legends

/ Adds a small description about the line the corner of the graph

a = [1, 2, 3]

a_sqre = [1, 4, 9]

a_cube = [1, 8, 27]

/ only adding label attribute won't do anything, make sure to call the legend()

plt.plot(a, a_sqre, color = "black", label = "a^2")

plt.plot(a, a_cube, color = "red", label = "a^3")

plt.legend()

plt.plot()

7) changing the axis values

x = [1, 2, 3]

y = [3, 4, 5]

plt.axis([0, 5, 3, 5]) -> pass a list in form of [x_axis_start, x_axis_end, y_axis_start, y_axis_end]

plt.plot(x, y, color = "black")

plt.show()

8) For a better understanding

x = [1, 2, 3]

y = [3, 4, 5]

plt.plot(x, y, color = "black")

plt.grid() -> gives a grid-like structure in the background similar to the graph paper

plt.show()

9) Adding some details at a particular point

x = [1, 2, 3]

y = [3, 4, 5]

plt.plot(x, y, color = "black")

/ font size is optional argument

/ syntax plt.text(x_point, y_point, "any text you want to add")

plt.text(1, 3, "Lowest value", fontsize = 10)

plt.text(2, 4, "Mid value", fontsize = 10)

plt.text(3, 5, "Highest value", fontsize = 10)

plt.show()

More from this blog

Untitled Publication

31 posts