खोज…


परिचय

Matplotlib ( https://matplotlib.org/) NumPy पर आधारित 2D प्लॉटिंग के लिए एक लाइब्रेरी है। यहाँ कुछ बुनियादी उदाहरण हैं। अधिक उदाहरण आधिकारिक दस्तावेज ( https://matplotlib.org/2.0.2/gallery.html और https://matplotlib.org/2.0.2/examples/index.html) के साथ-साथ http में भी देखे जा सकते हैं: //www.riptutorial.com/topic/881

मैटलपोटलिब में एक साधारण प्लॉट

यह उदाहरण दिखाता है कि कैसे एक सरल साइन वक्र बनाने के लिए Matplotlib का उपयोग किया जाता है

# Plotting tutorials in Python
# Launching a simple plot

import numpy as np
import matplotlib.pyplot as plt

# angle varying between 0 and 2*pi
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)                        # sine function

plt.plot(x, y)
plt.show()

एक नमूना साइन वक्र

एक साधारण भूखंड में अधिक सुविधाएँ जोड़ना: अक्ष लेबल, शीर्षक, अक्ष टिक, ग्रिड और किंवदंती

इस उदाहरण में, हम साइन वक्र प्लॉट लेते हैं और इसमें और सुविधाएँ जोड़ते हैं; जैसे शीर्षक, अक्ष लेबल, शीर्षक, अक्ष टिक, ग्रिड और किंवदंती।

# Plotting tutorials in Python
# Enhancing a plot

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)

# values for making ticks in x and y axis
xnumbers = np.linspace(0, 7, 15)
ynumbers = np.linspace(-1, 1, 11)

plt.plot(x, y, color='r', label='sin') # r - red colour
plt.xlabel("Angle in Radians")
plt.ylabel("Magnitude")
plt.title("Plot of some trigonometric functions")
plt.xticks(xnumbers)
plt.yticks(ynumbers)
plt.legend()
plt.grid()
plt.axis([0, 6.5, -1.1, 1.1]) # [xstart, xend, ystart, yend]
plt.show()

अक्ष लेबल, अक्ष टिक, शीर्षक, ग्रिड और किंवदंती के साथ एक बढ़ाया साइन प्लॉट

MATLAB के समान सुपरइम्पोजिशन द्वारा एक ही आकृति में कई प्लॉट बनाना

इस उदाहरण में, एक साइन वक्र और एक कोसाइन वक्र को एक दूसरे के शीर्ष पर भूखंडों को सुपरइम्पोज़ करके उसी आकृति में प्लॉट किया जाता है।

# Plotting tutorials in Python
# Adding Multiple plots by superimposition
# Good for plots sharing similar x, y limits
# Using single plot command and legend

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.cos(x)

# values for making ticks in x and y axis
xnumbers = np.linspace(0, 7, 15)
ynumbers = np.linspace(-1, 1, 11)

plt.plot(x, y, 'r', x, z, 'g') # r, g - red, green colour
plt.xlabel("Angle in Radians")
plt.ylabel("Magnitude")
plt.title("Plot of some trigonometric functions")
plt.xticks(xnumbers)
plt.yticks(ynumbers)
plt.legend(['sine', 'cosine'])
plt.grid()
plt.axis([0, 6.5, -1.1, 1.1]) # [xstart, xend, ystart, yend]
plt.show()

एक ही आंकड़े पर दो प्लॉट सुपरिंपोज किए गए

अलग-अलग प्लॉट कमांड के साथ प्लॉट सुपरइम्पोज़िशन का उपयोग करके एक ही आकृति में कई प्लॉट बनाना

पिछले उदाहरण के समान, यहां, अलग-अलग कथानक आदेशों का उपयोग करके एक साइन और कोसाइन वक्र को एक ही आकृति पर प्लॉट किया जाता है। यह अधिक पायथोनिक है और इसका उपयोग प्रत्येक प्लॉट के लिए अलग-अलग हैंडल प्राप्त करने के लिए किया जा सकता है।

# Plotting tutorials in Python
# Adding Multiple plots by superimposition
# Good for plots sharing similar x, y limits
# Using multiple plot commands
# Much better and preferred than previous

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.cos(x)

# values for making ticks in x and y axis
xnumbers = np.linspace(0, 7, 15)
ynumbers = np.linspace(-1, 1, 11)

plt.plot(x, y, color='r', label='sin') # r - red colour
plt.plot(x, z, color='g', label='cos') # g - green colour
plt.xlabel("Angle in Radians")
plt.ylabel("Magnitude")
plt.title("Plot of some trigonometric functions")
plt.xticks(xnumbers)
plt.yticks(ynumbers)
plt.legend()
plt.grid()
plt.axis([0, 6.5, -1.1, 1.1]) # [xstart, xend, ystart, yend]
plt.show()

प्लॉट सुपरइम्पोज़िशन का उपयोग करके साइन और कोसाइन का प्लॉट

सामान्य X- अक्ष लेकिन अलग Y- अक्ष के साथ भूखंड: जुड़वाँ का उपयोग करना ()

इस उदाहरण में, हम अलग-अलग y- अक्ष वाले एक सामान्य x- अक्ष के साथ एक ही वक्र में साइन वक्र और हाइपरबोलिक साइन वक्र प्लॉट करेंगे। यह ट्विनक्स () कमांड के उपयोग से पूरा हुआ है।

# Plotting tutorials in Python
# Adding Multiple plots by twin x axis
# Good for plots having different y axis range
# Separate axes and figure objects
# replicate axes object and plot curves
# use axes to set attributes

# Note:
# Grid for second curve unsuccessful : let me know if you find it! :(

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)

# separate the figure object and axes object
# from the plotting object
fig, ax1 = plt.subplots()

# Duplicate the axes with a different y axis
# and the same x axis
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis

# plot the curves on axes 1, and 2, and get the curve handles
curve1, = ax1.plot(x, y, label="sin", color='r')
curve2, = ax2.plot(x, z, label="sinh", color='b')

# Make a curves list to access the parameters in the curves
curves = [curve1, curve2]

# add legend via axes 1 or axes 2 object.
# one command is usually sufficient
# ax1.legend() # will not display the legend of ax2
# ax2.legend() # will not display the legend of ax1
ax1.legend(curves, [curve.get_label() for curve in curves])
# ax2.legend(curves, [curve.get_label() for curve in curves]) # also valid

# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()

आम एक्स अक्ष के साथ प्लॉट

आम Y- अक्ष और अलग X- अक्ष के साथ सुतली का उपयोग करके प्लॉट ()

इस उदाहरण में, एक प्लॉट जिसमें सामान्य वाई-एक्सिस वाले वक्र हैं लेकिन ट्विन () विधि का उपयोग करके अलग-अलग एक्स-एक्सिस का प्रदर्शन किया जाता है। इसके अलावा, कुछ अतिरिक्त विशेषताएं जैसे कि शीर्षक, किंवदंती, लेबल, ग्रिड, अक्ष टिक और रंग प्लॉट में जोड़े जाते हैं।

# Plotting tutorials in Python
# Adding Multiple plots by twin y axis
# Good for plots having different x axis range
# Separate axes and figure objects
# replicate axes object and plot curves
# use axes to set attributes

import numpy as np
import matplotlib.pyplot as plt

y = np.linspace(0, 2.0*np.pi, 101)
x1 = np.sin(y)
x2 = np.sinh(y)

# values for making ticks in x and y axis
ynumbers = np.linspace(0, 7, 15)
xnumbers1 = np.linspace(-1, 1, 11)
xnumbers2 = np.linspace(0, 300, 7)

# separate the figure object and axes object
# from the plotting object
fig, ax1 = plt.subplots()

# Duplicate the axes with a different x axis
# and the same y axis
ax2 = ax1.twiny() # ax2 and ax1 will have common y axis and different x axis

# plot the curves on axes 1, and 2, and get the axes handles
curve1, = ax1.plot(x1, y, label="sin", color='r')
curve2, = ax2.plot(x2, y, label="sinh", color='b')

# Make a curves list to access the parameters in the curves
curves = [curve1, curve2]

# add legend via axes 1 or axes 2 object.
# one command is usually sufficient
# ax1.legend() # will not display the legend of ax2
# ax2.legend() # will not display the legend of ax1
# ax1.legend(curves, [curve.get_label() for curve in curves])
ax2.legend(curves, [curve.get_label() for curve in curves]) # also valid

# x axis labels via the axes
ax1.set_xlabel("Magnitude", color=curve1.get_color())
ax2.set_xlabel("Magnitude", color=curve2.get_color())

# y axis label via the axes
ax1.set_ylabel("Angle/Value", color=curve1.get_color())
# ax2.set_ylabel("Magnitude", color=curve2.get_color()) # does not work
# ax2 has no property control over y axis

# y ticks - make them coloured as well
ax1.tick_params(axis='y', colors=curve1.get_color())
# ax2.tick_params(axis='y', colors=curve2.get_color()) # does not work
# ax2 has no property control over y axis

# x axis ticks via the axes
ax1.tick_params(axis='x', colors=curve1.get_color())
ax2.tick_params(axis='x', colors=curve2.get_color())

# set x ticks
ax1.set_xticks(xnumbers1)
ax2.set_xticks(xnumbers2)

# set y ticks
ax1.set_yticks(ynumbers)
# ax2.set_yticks(ynumbers) # also works

# Grids via axes 1 # use this if axes 1 is used to
# define the properties of common x axis
# ax1.grid(color=curve1.get_color())

# To make grids using axes 2
ax1.grid(color=curve2.get_color())
ax2.grid(color=curve2.get_color())
ax1.xaxis.grid(False)

# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()

आम y- अक्ष और अलग x- अक्ष के साथ प्लॉट



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow