Importing matplotlib.pyplot and plotting (two-dimensional plots): range(), subplot(), legend(), columns(), len() functions

matplotlib.pyplot (plt તરીકે import થયેલું) lists કે DataFrame નાં ખાનાંમાંથી 2-D આલેખ દોરે છે: plot() દોરે છે, legend() લેબલ આપે છે, subplot() એક જ figure માં અનેક charts ગોઠવે છે, અને show() એને પડદા પર મૂકે છે.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

સંખ્યાઓનું કોષ્ટક કોઈ વાંચતું નથી

ResultDesk નું describe() નું output આંકડાકીય રીતે સંપૂર્ણ છે, અને આચાર્યની આંખો બરાબર ચાર સેકન્ડમાં ઝાંખી પડી જાય છે.

પછી તમે નીચે ઢળતી એક જ લીટી દેખાડો છો: ત્રણ આંતરિક કસોટીઓમાં ઘટતી DBMS ની સરેરાશ, અને આખો ઓરડો આગળ ઝૂકે છે. એ જ data. જુદું પાત્ર.

માણસો કોષ્ટકો કરતાં ચિત્રો સેંકડો ગણી ઝડપથી પ્રક્રિયા કરે છે. એકમ 5 એ રૂપાંતર વિશે છે, અને એનું engine છે Python નું પ્રમાણભૂત આલેખનું library: matplotlib.

Theory

આલેખના કાગળનું કાર્યમેજ

pyplot ને આલેખના કાગળના કાર્યમેજ તરીકે વિચારો:

plt.plot() હાલના પાના પર પેન્સિલથી લીટી દોરે છે. ફરી બોલાવો: બીજી લીટી, એ જ પાનું. title/xlabel/ylabel મથાળાં લખે છે. legend() રંગની ચાવી ટાંકે છે. અને plt.show() પાનું ઊંચું ન કરે ત્યાં સુધી કશું કાર્યમેજ છોડતું નથી.

subplot() એ શક્તિશાળી ચાલ છે: પાનાને જાળીમાં લીટીઓ પાડો અને દરેક ખાનામાં જુદો chart દોરો.

Theory

pyplot નો શબ્દભંડોળ

import matplotlib.pyplot as plt: સાર્વત્રિક alias (એક વાર pip install matplotlib).

  • plt.plot(x, y): બિંદુઓમાંથી 2-D લીટી દોરો.
  • plt.title / plt.xlabel / plt.ylabel: chart અને બંને અક્ષને નામ આપો.
  • plt.legend(): ચાવી: દરેક plot ના label= ના argument માંથી બને છે.
  • plt.show(): figure દોરો: scripts એના વગર કશું દેખાડતી નથી.
  • plt.savefig('report.png'): દેખાડવાને બદલે સાચવો.

તમે પહેલેથી જાણો છો એ મદદગારો: range(n) એ x નાં સ્થાન 0..n-1 બનાવે છે, len(df) હરોળ ગણે છે, df.columns શું દોરી શકાય એની યાદી કરે છે.

Practical

પહેલો plot, પછી 1x2 ની જાળી

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('marks.csv')

# one chart: DBMS scores across roll numbers
dbms = df[df['subject'] == 'DBMS']
plt.plot(dbms['roll'], dbms['score'], label='DBMS')
plt.plot(dbms['roll'], dbms['score'] * 0 + dbms['score'].mean(),
         label='class mean')          # a flat reference line
plt.title('DBMS scores by roll')
plt.xlabel('Roll number')
plt.ylabel('Score')
plt.legend()                          # uses the label= texts
plt.show()                            # nothing appears without this

# two charts on ONE figure: subplot(rows, cols, index), index from 1
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)                  # left cell
plt.plot(range(len(dbms)), dbms['score'])
plt.title('By position (range/len)')
plt.subplot(1, 2, 2)                  # right cell
plt.plot(dbms['roll'], dbms['score'])
plt.title('By roll number')
plt.show()

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

કોરી બારીનું રહસ્ય

એક student ની script સુંદર plot બનાવે છે: plot(), title(), લેબલ, legend()... અને ચલાવતાં કશું છપાતું નથી, કશું દેખાતું નથી, કોઈ ભૂલ નહીં. Tap કરતાં પહેલાં: કઈ એક લીટી ખૂટે છે, અને એ પહેલાંનાં બધાં calls ખરેખર શું કરતાં હતાં?

Show the answer

plt.show() ખૂટે છે. પહેલાંનું દરેક call memory માંના figure પર દોરતું હતું: કાર્યમેજનું એ પાનું જે ક્યારેય ઊંચું જ ન થયું. Script પૂરી થાય છે, figure ચૂપચાપ ફેંકાઈ જાય છે.

(Jupyter ની notebooks માં figures ઘણી વાર આપોઆપ દેખાય છે, અને બરાબર એટલે જ students સાદી scripts માં show() ભૂલે છે: પરીક્ષાનો જવાબ છે: pyplot memory માં figures બનાવે છે; show() એમને દોરે છે.)

Quiz

plt.subplot(2, 2, 1) બોલાવાયું. આગલો plot figure ના કયા ખાનામાં ઊતરે છે?

  1. ઉપર ડાબે: 2x2 ની જાળી, અને index 1 એ 1 થી હરોળવાર ગણતાં **પહેલું** ખાનું છે
  2. નીચે જમણે: index ઊલટો ગણાય છે
  3. ઉપર ડાબે, કારણ કે index 0 એટલે પહેલું
  4. એ ભૂલ આપે છે: subplot ને 0 થી index જોઈએ
Show the answer

ઉપર ડાબે: 2x2 ની જાળી, અને index 1 એ 1 થી હરોળવાર ગણતાં **પહેલું** ખાનું છે

subplot(rows, cols, index) એ rows x cols ની જાળી પાડે છે અને index નંબરનું ખાનું પસંદ કરે છે, 1 થી ગણતાં, ડાબેથી જમણે, ઉપરથી નીચે: (2,2,1) ઉપર ડાબે, (2,2,2) ઉપર જમણે, (2,2,3) નીચે ડાબે, (2,2,4) નીચે જમણે. 1 થી ગણવાનું શૂન્યથી શરૂ થતી lists પર તાલીમ પામેલી Python ની સમજને ઠોકર ખવડાવે છે: વિકલ્પ C અને D બરાબર એ જ સમજ છે, અને બરાબર એ જ ગુમાવેલો mark.

Watch out

Chart મારનારા ત્રણ

plt.show() નહીં: chart ને બદલે મૌન (scripts માં, notebooks માં નહીં).

label= નાં arguments નહીં: plt.legend() ખાલી ખોખું દોરે છે કે ચેતવણી આપે છે: ચાવીમાં તમને જોઈતા દરેક plot() ને label='...' જોઈએ.

લેબલ વગરના અક્ષ: title/xlabel/ylabel વગરના chart બદલ પરીક્ષકો marks કાપે છે, અને એ સાચા છે: લેબલ વગરનો chart અહેવાલ નહીં, અફવા છે. અભ્યાસક્રમ ax.set_title() નું નામ પણ આપે છે: એ જ વિચારની પદાર્થ-શૈલીની જોડણી, જે fig, ax = plt.subplots() સાથે વપરાય છે.

Theory

એક ભાત, આગળ ચાર charts

આ એકમનો દરેક બાકીનો પાઠ એ જ હાડપિંજર છે જેમાં એક ક્રિયાપદ બદલાય છે: બિંદુઓના વાદળ માટે scatter(), વલણો માટે plot(), distributions માટે hist(), શ્રેણીની સરખામણી માટે bar(). હાડપિંજર એક વાર શીખો અને પછીના ચાર પાઠ દરેક એક નવું ક્રિયાપદ વત્તા ક્યારે વાપરવું એનો નિર્ણય બની જાય છે.

Summary

Key takeaways

  • import matplotlib.pyplot as plt; pandas ની જેમ pip થી install થાય છે.
  • હાડપિંજર: plot(x, y, label=...), title(), xlabel(), ylabel(), legend(), show().
  • Scripts માં plt.show() વગર કશું દોરાતું નથી; savefig() file માં લખે છે.
  • વારંવારનાં plot() નાં calls એક જ figure પર લીટીઓ ચઢાવે છે; legend() એમને label= થી ચાવી આપે છે.
  • subplot(rows, cols, index) charts ગોઠવે છે: index 1 થી ગણાય છે, હરોળવાર.
  • range() અને len() x નાં સ્થાન બનાવે છે; DataFrame નાં ખાનાં સીધાં દોરાય છે.
  • Memory hook: પાના પર પેન્સિલ, show() થી એને ઊંચું કરો.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Data Visualization using dataframe

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Importing matplotlib.pyplot and plotting (two-dimensional plots): range(), subplot(), legend(), columns(), len() functions · Database Handling using Python · Gri-Learn