Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet192/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   188   189   190   191   192   193   194   195   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Line Plot


Let’s start with the most basic chart we can create, the line plot:

1| # creating a line plot using x and y coords
3| x, y = [ 1600, 1700, 1800, 1900, 2000 ] , [ 0.2, 0.5, 1.1, 2.2, 7.7 ]
5| plt.plot(x, y) # creates the line
7| plt.title("World Population Over Time")
8| plt.xlabel("Year")
9| plt.ylabel("Population (billions)")
11| plt.show( )

Go ahead and run the cell. To start, we create our x and y coordinates for plotting.
The plot() method allows us to plot a single line; it just needs the coordinates passed in. Lines 7, 8, and 9 are all for customizing the chart and its appearance. Lastly, we use the show() method to render the chart. You should output a chart like Figure 10-1.

Figure 10-1. Single line plot of population data
When you want to add more lines to the chart, you simply apply as many plot() methods as necessary. Let’s add some more customization to each plot line this time:

1| # creating a line plot with multiple lines
3| x1, y1 = [ 1600, 1700, 1800, 1900, 2000 ] , [ 0.2, 0.5, 1.1, 2.2, 7.7 ]
4| x2, y2 = [ 1600, 1700, 1800, 1900, 2000 ] , [ 1, 1, 2, 3, 4 ]
6| plt.plot(x1, y1, "rx-", label="Actual") # create a red solid line with x dots
7| plt.plot(x2, y2, "bo--", label="Fake") # create a blue dashed line with circle dots
9| plt.title("World Population Over Time")
10| plt.xlabel("Year")
11| plt.ylabel("Population (billions)")
12| plt.legend( ) # shows labels in best corner
14| plt.show( )

Go ahead and run the cell. By adding a second set of coordinates, we’re able to plot a second line using the plot() method on line 7. We also specified how the lines should render using shorthand syntax. For the third argument in the plot method, we can pass a string that represents the color, symbols for dots, and the line style. Finally, we added a label to each line for making it easy to read the multiline chart, and we’re able to show it by calling the legend() method. The output should look like Figure 10-2.
CHapter 10 INtroduCtIoN to data aNalYsIs

Figure 10-2. Multiline plot of population data

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   188   189   190   191   192   193   194   195   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish