DEV Community

Soujanya Satpute
Soujanya Satpute

Posted on

Matplotlib - Brief

Plotting Normal Graph

  • pyplot.plot() function: If you call plot() one after another, Next graph will be printed on same graph window.
plt.plot(x_axis_column, y_axis_column)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Image description

  • Labeling Graph Label gives different names to Graph as we wanted it. Legend: Area that describe graph elements. This function have attributes for styling and locating legend on graph screen
plt.plot(x_axis_column, y_axis_column, label='Label Name')
plt.title('Title of Graph')
plt.ylabel('Ylabel')
plt.xlabel('Ylabel')
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Image description

plt.text(xPosition,yPosition,'Text')

Enter fullscreen mode Exit fullscreen mode

Be careful while putting positional co-ordinates. Don't set it out of range.

  • Styling Graph: In .plt.plot() we have attributes like color,linestyle, marker. There are multiple default styles provided by matplotlib to try on to your graphs.

Plotting Scatter Plot

  • Only change from .plot to .scatter And Styling can be applied same as normal Graph
plt.scatter(x_axis_column, y_axis_column, color = 'red')
Enter fullscreen mode Exit fullscreen mode

Image description

  • You can add Crazy styling with different Background image. Resource here

Plotting Bar Graph

Can you guess how to do that?
plt.bar()? Yes Exactly!

plt.bar(x_axis_column, y_axis_column)

Enter fullscreen mode Exit fullscreen mode

Image description

Plotting histogram

First Question: What's difference between Bar graph and histogram?
Histogram: display the frequency of numerical data, It means in given column of database what is the frequency of particular number.
It contains non-discrete values.
Bar graph: compare different categories of data. discrete values can work.
Much clear difference here
Now Actual Plotting:

plt.hist(column_data_for_histogram, bins=50, range = (5,35))

Enter fullscreen mode Exit fullscreen mode

Image description
Changing Value of bins
Bins are number of columns or towers in the histogram. Default value of these bins is 10. We can modify in the code.
Defining Range of histogram
Manually selecting range for histogram for where to start and end.

Top comments (0)