Printing the list has been dealt many times. But sometimes we need a different format to get the output of list. This also has application in getting a transpose of matrix. Printing list vertically also has application in web development. Lets discuss certain ways in which this task can be achieved.
Method #1 : Using Naive Method The naive method can be used to print the list vertically vis. using the loops and printing each index element of each list successively will help us achieve this task.
Python3
# Python3 code to demonstrate
# Vertical list print
# using naive method
# initializing list
test_listThe original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 100 The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 101The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 102The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 103The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 104The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 103The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 106The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 107The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 104The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 103The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 100The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 103The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 102The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 107The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 102The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 103The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 106The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 103The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 108The original list is: [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 109
Matplotlib is a popular python library used for plotting, It provides an object-oriented API to render GUI plots. Plotting a horizontal line is fairly simple, The following code shows how it can be done.
Making a single vertical line
Method #1: Using ()
This function adds the vertical lines across the axes of the plot
Syntax: matplotlib.pyplot.axvline(x, color, xmin, xmax, linestyle)
Parameters:
- x: Position on X axis to plot the line, It accepts integers.
- xmin and xmax: scalar, optional, default: 0/1. It plots the line in the given range
- color: color for the line, It accepts a string. eg ‘r’ or ‘b’ .
- linestyle: Specifies the type of line, It accepts a string. eg ‘-‘, ‘–‘, ‘-.’, ‘:’, ‘None’, ‘ ‘, ”, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’
Python3
# importing the modules
import matplotlib.pyplot as plt
import numpy as np
# specifying the plot size
plt.figure(figsize= (10import0import1import2
import3
import4= import6import7= import9matplotlib.pyplot as plt0= matplotlib.pyplot as plt2matplotlib.pyplot as plt3
matplotlib.pyplot as plt4
matplotlib.pyplot as plt5
Output:
Method #2: Using vlines()
matplotlib.pyplot.vlines() is a function used in the plotting of a dataset. In matplotlib.pyplot.vlines(), vlines is the abbreviation for vertical lines. What this function does is very much clear from the expanded form, which says that function deals with the plotting of the vertical lines across the axes.
Syntax: vlines(x, ymin, ymax, colors, linestyles)
Parameters:
- x: Position on X axis to plot the line, It accepts integers.
- xmin and xmax: scalar, optional, default: 0/1. It plots the line in the given range
- color: color for the line, It accepts a string. eg ‘r’ or ‘b’ .
- linestyle: Specifies the type of line, It accepts a string. eg ‘-‘, ‘–‘, ‘-.’, ‘:’, ‘None’, ‘ ‘, ”, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’
Python3
matplotlib.pyplot as plt6
import matplotlib.pyplot as plt
import numpy as np
import1
import2= import4import5import0import7import8
import9
plt.figure(figsize= (10import0import6import2
numpy as np7
numpy as np8= # specifying the plot size0# specifying the plot size1= # specifying the plot size3# specifying the plot size4= # specifying the plot size6# specifying the plot size7
# specifying the plot size8# specifying the plot size9= plt.figure(figsize1import0
# specifying the plot size8plt.figure(figsize4= plt.figure(figsize6matplotlib.pyplot as plt3
matplotlib.pyplot as plt5
Output:
Method #3: Using plot()
The plot() function in pyplot module of matplotlib library is used to make a 2D hexagonal binning plot of points x, y.
Syntax : plot(x_points, y_points, scaley = False)
Parameters:
- x_points/y_points: points to plot
- scalex/scaley: Bool, These parameters determine if the view limits are adapted to the data limits
Python3
plt.figure(figsize9
import matplotlib.pyplot as plt
import9
plt.figure(figsize= (10import0import1import2
(0
(1# specifying the plot size3import0# specifying the plot size3(5# specifying the plot size3import0import5(9= 101matplotlib.pyplot as plt3
103
104
matplotlib.pyplot as plt5
Output:
Plotting multiple lines with the legend
The below methods can be used for plotting multiple lines in Python.
Method #1: Using ()
Python3
# importing the modules
import matplotlib.pyplot as plt
import numpy as np
# specifying the plot size
plt.figure(figsize= (10import0import1import2
import3
import4= import6import7= import9matplotlib.pyplot as plt0= matplotlib.pyplot as plt2matplotlib.pyplot as plt3
import20
import21
import4= import24# specifying the plot size1= import27# specifying the plot size4= import30import7= import33import0
import35plt.figure(figsize4= import38matplotlib.pyplot as plt3
import40
import41= (import44import0import5import47= matplotlib.pyplot as plt78matplotlib.pyplot as plt3