Simple plotting in python

Simple plotting in python DEFAULT

Matplotlib - Simple Plot



In this chapter, we will learn how to create a simple plot with Matplotlib.

We shall now display a simple line plot of angle in radians vs. its sine value in Matplotlib. To begin with, the Pyplot module from Matplotlib package is imported, with an alias plt as a matter of convention.

import matplotlib.pyplot as plt

Next we need an array of numbers to plot. Various array functions are defined in the NumPy library which is imported with the np alias.

import numpy as np

We now obtain the ndarray object of angles between 0 and 2π using the arange() function from the NumPy library.

x = np.arange(0, math.pi*2, 0.05)

The ndarray object serves as values on x axis of the graph. The corresponding sine values of angles in x to be displayed on y axis are obtained by the following statement −

y = np.sin(x)

The values from two arrays are plotted using the plot() function.

plt.plot(x,y)

You can set the plot title, and labels for x and y axes.

You can set the plot title, and labels for x and y axes. plt.xlabel("angle") plt.ylabel("sine") plt.title('sine wave')

The Plot viewer window is invoked by the show() function −

plt.show()

The complete program is as follows −

from matplotlib import pyplot as plt import numpy as np import math #needed for definition of pi x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) plt.plot(x,y) plt.xlabel("angle") plt.ylabel("sine") plt.title('sine wave') plt.show()

When the above line of code is executed, the following graph is displayed −

Simple Plot

Now, use the Jupyter notebook with Matplotlib.

Launch the Jupyter notebook from Anaconda navigator or command line as described earlier. In the input cell, enter import statements for Pyplot and NumPy −

from matplotlib import pyplot as plt import numpy as np

To display plot outputs inside the notebook itself (and not in the separate viewer), enter the following magic statement −

%matplotlib inline

Obtain x as the ndarray object containing angles in radians between 0 to 2π, and y as sine value of each angle −

import math x = np.arange(0, math.pi*2, 0.05) y = np.sin(x)

Set labels for x and y axes as well as the plot title −

plt.xlabel("angle") plt.ylabel("sine") plt.title('sine wave')

Finally execute the plot() function to generate the sine wave display in the notebook (no need to run the show() function) −

plt.plot(x,y)

After the execution of the final line of code, the following output is displayed −

Final Line of Code
Sours: https://www.tutorialspoint.com/matplotlib/matplotlib_simple_plot.htm

Graph Plotting in Python | Set 1

This series will introduce you to graphing in python with Matplotlib, which is arguably the most popular graphing and data visualization library for Python.
Installation
Easiest way to install matplotlib is to use pip. Type following command in terminal: 
 

pip install matplotlib

OR, you can download it from here and install it manually. 
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Getting started ( Plotting a line)



Python

 

 

 

 

 

Output: 
 

mp1

The code seems self explanatory. Following steps were followed: 

  • Define the x-axis and corresponding y-axis values as lists.
  • Plot them on canvas using .plot() function.
  • Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
  • Give a title to your plot using .title() function.
  • Finally, to view your plot, we use .show() function.

 

Plotting two or more lines on same plot

Python

 

 

 

 

 

Output: 
 

mp2

 



  • Here, we plot two lines on same graph. We differentiate between them by giving them a name(label) which is passed as an argument of .plot() function.
  • The small rectangular box giving information about type of line and its color is called legend. We can add a legend to our plot using .legend() function.

 

Customization of Plots

Here, we discuss some elementary customizations applicable on almost any plot.

Python

 

 

 

 

 

 

Output: 
 

mp3

As you can see, we have done several customizations like 

  • setting the line-width, line-style, line-color.
  • setting the marker, marker’s face color, marker’s size.
  • overriding the x and y axis range. If overriding is not done, pyplot module uses auto-scale feature to set the axis range and scale.

  
 

Bar Chart

Python

 

 

 

 

 

 

Output :
 

mp4



 

  • Here, we use plt.bar() function to plot a bar chart.
  • x-coordinates of left side of bars are passed along with heights of bars.
  • you can also give some name to x-axis coordinates by defining tick_labels

 

Histogram

Python

 

 

 

 

 

 

Output:
 

mp5

 

  • Here, we use plt.hist() function to plot a histogram.
  • frequencies are passed as the ages list.
  • Range could be set by defining a tuple containing min and max value.
  • Next step is to “bin” the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. Here we have defined bins = 10. So, there are a total of 100/10 = 10 intervals.

 

Scatter plot

 

Python

 

 

 

 

Output:
 



mp6

 

  • Here, we use plt.scatter() function to plot a scatter plot.
  • Like a line, we define x and corresponding y – axis values here as well.
  • marker argument is used to set the character to use as marker. Its size can be defined using s parameter.

 

Pie-chart

 

Python

 

 

 

 

 

 

Output of above program looks like this:
 

mp7

 

  • Here, we plot a pie chart by using plt.pie() method.
  • First of all, we define the labels using a list called activities.
  • Then, portion of each label can be defined using another list called slices.
  • Color for each label is defined using a list called colors.
  • shadow = True will show a shadow beneath each label in pie-chart.
  • startangle rotates the start of the pie chart by given degrees counterclockwise from the x-axis.
  • explode is used to set the fraction of radius with which we offset each wedge.
  • autopct is used to format the value of each label. Here, we have set it to show the percentage value only upto 1 decimal place.

 

Plotting curves of given equation



 

Python

 

 

 

Output of above program looks like this: 
 

mp8

Here, we use NumPy which is a general-purpose array-processing package in python. 
 

  • To set the x – axis values, we use np.arange() method in which first two arguments are for range and third one for step-wise increment. The result is a numpy array.
  • To get corresponding y-axis values, we simply use predefined np.sin() method on the numpy array.
  • Finally, we plot the points by passing x and y arrays to the plt.plot() function.

So, in this part, we discussed various types of plots we can create in matplotlib. There are more plots which haven’t been covered but the most significant ones are discussed here – 
 

 

This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




Sours: https://www.geeksforgeeks.org/graph-plotting-in-python-set-1/
  1. Baseball coloring pages
  2. 21 imdb
  3. Speaker cabinet covering
  4. Orange peel roll on texture
  5. Dreadlock jewelry

Matplotlib Plotting

❮ PreviousNext ❯


Plotting x and y points

The function is used to draw points (markers) in a diagram.

By default, the function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.

Example

Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

Try it Yourself »

The x-axis is the horizontal axis.

The y-axis is the vertical axis.


Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.

Example

Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, 'o')
plt.show()

Result:

Try it Yourself »

You will learn more about markers in the next chapter.


Multiple Points

You can plot as many points as you like, just make sure you have the same number of points in both axis.

Example

Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

Try it Yourself »

Default X-Points

If we do not specify the points in the x-axis, they will get the default values 0, 1, 2, 3, (etc. depending on the length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

Example

Plotting without x-points:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()

Result:

Try it Yourself »

The x-points in the example above is [0, 1, 2, 3, 4, 5].


❮ PreviousNext ❯


Sours: https://www.w3schools.com/python/matplotlib_plotting.asp
Python Tutorials - Making a Simple Plot Using pyplot module - matplotlib

.

In python plotting simple

.

SIMPLE PLOT IN MATPLOTLIB -- LINE PLOT IN MATPLOTLIB -- MATPLOTLIB LIBRARY -- PYTHON PROGRAMMING

.

You will also be interested:

.



210 211 212 213 214