कोर्स
डेटा विज़ुअलाइज़ेशन डेटा विश्लेषकों और डेटा वैज्ञानिकों के लिए जटिल जानकारी प्रस्तुत करने और सार्थक अंतर्दृष्टि निकालने के सबसे अच्छे तरीकों में से एक है (यदि सबसे अच्छा न हो)। हितधारकों के साथ संवाद करने के अपने अवसर का अधिकतम लाभ उठाने के लिए, मैंने Matplotlib colormaps के बारे में उपयोगी जानकारी संकलित की है।
जैसा कि आप इस लेख में देखेंगे, Matplotlib Python में एक अत्यंत बहुमुखी और विस्तार योग्य विज़ुअलाइज़ेशन लाइब्रेरी है। यह सरल चार्ट से लेकर पूरी तरह इंटरैक्टिव विज़ुअल तक हर विकल्प प्रदान करती है। यदि आप सामान्य रूप से Python से परिचित नहीं हैं, तो मैं आपको हमारी Introduction to Python कोर्स में नामांकन करने के लिए प्रोत्साहित करता/करती हूँ ताकि हम शुरू करने से पहले इसकी बुनियाद समझ सकें। व्यक्तिगत रूप से, मैं Python Cheat Sheet भी पास रखता/रखती हूँ क्योंकि यह सामान्य फ़ंक्शंस के लिए एक उपयोगी संदर्भ है।
सही Matplotlib Colormap चुनना
सबसे पहले विचार करने वाली बात यह है कि sequential, diverging, या categorical colormaps में से किसे चुनें। Colormap चुनते समय अन्य महत्वपूर्ण कारक हैं perceptual uniformity—जिसका अर्थ है कि डेटा मानों में समान अंतर रंग में समान अंतर के रूप में दिखाई दे—और colorblind-friendly colormap विकल्पों का उपयोग, ताकि आपकी विज़ुअल सभी दर्शकों के लिए सुलभ हों।
Colormaps चुनते समय विभिन्न डोमेन मानकों पर भी विचार करें। उदाहरण के लिए, महासागरीय अध्ययनों में पानी की विभिन्न गहराइयों को दिखाने के लिए नीले रंग की अलग-अलग छायाएँ उपयोग की जाती हैं। यदि आप अपनी विज़ुअलाइज़ेशन के लिए सही colormap को लेकर अनिश्चित हैं, तो मार्गदर्शन के लिए विभिन्न टूल और संसाधन उपलब्ध हैं। विशेष रूप से, हमारा Introduction to Data Visualization with Matplotlib कोर्स आपको कई परिदृश्यों पर काम करने में मदद करता है।
Matplotlib में अलग-अलग Colormaps
Python में, matplotlib.colormaps मॉड्यूल बिल्ट-इन colormaps तक पहुँच प्रदान करता है, जो आपको अपने प्रोजेक्ट के लिए सबसे उपयुक्त स्कीम चुनने में मदद करता है। निम्नलिखित सबसे सामान्य श्रेणियाँ हैं:
Sequential colormaps
Sequential colormaps क्रमबद्ध डेटा का प्रतिनिधित्व करते हैं जो निम्न से उच्च मानों तक प्रगति करता है। ये हल्के से गहरे रंगों की ओर संक्रमण करते हैं, विभिन्न स्तरों पर डेटा परिमाण दिखाते हैं। Sequential colormap का एक उदाहरण तापमान डेटा के हीटमैप्स में मिलता है, जहाँ हल्के रंग ठंडे तापमान और गहरे रंग अधिक गर्म तापमान का प्रतिनिधित्व करते हैं।
मान लें कि आपके पास तारीख और तापमान वाले कॉलम के साथ एक डेटासेट है। निम्नलिखित कोड तापमान के लिए एक हीटमैप बनाएगा।
# Import required libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
# Step 1: Extract month and day information from the date column
calendar_data = temperature_data.copy() # Create a copy to work with
calendar_data['Month'] = calendar_data['Date'].dt.strftime('%B')
calendar_data['Day'] = calendar_data['Date'].dt.day
# Step 2: Define the order of months for a natural calendar sequence
month_names = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
# Convert the 'Month' column to a categorical type with the specified order
calendar_data['Month'] = pd.Categorical(calendar_data['Month'], categories=month_names, ordered=True)
# Step 3: Pivot the data to create a structured table for the heatmap
monthly_temperature_table = calendar_data.pivot(index='Month', columns='Day', values='Temperature')
# Step 4: Set up the plot for the heatmap
plt.figure(figsize=(12, 8))
# Define a custom normalization for temperature range (optional)
norm = mcolors.Normalize(vmin=15, vmax=35)
# Create the heatmap with a sequential colormap
heatmap = plt.imshow(monthly_temperature_table, cmap='YlOrRd', aspect='auto', norm=norm)
# Add color bar to represent temperature
colorbar = plt.colorbar(heatmap, orientation='horizontal')
colorbar.set_label('Temperature (°C)', labelpad=10)
# Label the axes
plt.xlabel('Day of the Month')
plt.ylabel('Month')
# Set y-ticks to display month names instead of numbers
plt.yticks(ticks=np.arange(len(month_names)), labels=month_names)
# Add a title to the heatmap
plt.title('Monthly Temperature Heatmap (Sequential Colormap)')
# Display the plot
plt.grid(False)
plt.show()

Matplotlib sequential colormaps का विज़ुअलाइज़ेशन उदाहरण। छवि: लेखक
Diverging colormaps
Diverging colormaps उन डेटा को हाईलाइट करते हैं जिनमें केंद्रीय मान से दोनों दिशाओं में विचलन होता है। इनमें सकारात्मक और नकारात्मक दिशाओं के लिए परस्पर विरोधी रंग होते हैं। Diverging colormaps का एक सामान्य उपयोग वित्तीय डेटा में लाभ और हानि को दर्शाने के लिए होता है।
# Set 'Month' as the index
profit_loss_df.set_index('Month', inplace=True)
# Set the figure size
plt.figure(figsize=(8, 4))
# Create a diverging colormap
cmap = plt.cm.RdYlGn
# Display the data as a heatmap using imshow
im = plt.imshow(profit_loss_df.T, cmap=cmap, aspect='auto', vmin=-np.max(np.abs(profit_loss_data)), vmax=np.max(np.abs(profit_loss_data)))
# Add color bar with label
cbar = plt.colorbar(im)
cbar.set_label('Profit/Loss ($)')
# Add annotations to each cell
for i, month in enumerate(profit_loss_df.index):
plt.text(i, 0, f'{profit_loss_df.loc[month, "Profit/Loss"]:.2f}', ha='center', va='center', color='black')
# Set x-axis and y-axis labels and ticks
plt.xlabel('Month')
plt.ylabel('') # No label needed for y-axis
plt.xticks(ticks=range(len(profit_loss_df.index)), labels=profit_loss_df.index, rotation=45)
plt.yticks([]) # Hide y-axis ticks as we only have one row
# Add title
plt.title('Profit/Loss Heatmap (Diverging Colormap)')
# Adjust layout for better display
plt.tight_layout()
# Show plot
plt.show()

Matplotlib diverging colormap का विज़ुअलाइज़ेशन उदाहरण। छवि: लेखक।
Cyclic colormaps
Cyclic colormaps पुनरावृत्त या चक्रीय पैटर्न का प्रतिनिधित्व करने वाले डेटा के लिए उपयोगी हैं, जैसे कोण, तरंग के फेज़, या दिन का समय। Cyclic colormap का एक उदाहरण साइन वेव जैसी आवधिक फ़ंक्शन के फेज़ को विज़ुअलाइज़ करना है।
# Create data for a sine wave
x = np.linspace(0, 2 * np.pi, 100) # X values from 0 to 2π (one complete cycle)
y = np.sin(x) # Y values (sine of X)
# Create a figure and axis
plt.figure(figsize=(10, 6))
# Create a scatter plot with a cyclic colormap
# Color points by their position in the sine wave cycle
points = plt.scatter(x, y, c=x, cmap='twilight', edgecolor='black')
# Add a color bar to show the phase of the wave
cbar = plt.colorbar(points)
cbar.set_label('Wave Phase (Radians)', rotation=270, labelpad=15)
# Label the axes
plt.xlabel('Angle (Radians)')
plt.ylabel('Sine Value')
# Add a title
plt.title('Sine Wave with Cyclic Colormap (Twilight)')
# Display the plot
plt.grid(True)
plt.show()

Matplotlib cyclic colormap का विज़ुअलाइज़ेशन उदाहरण। छवि: लेखक।
Qualitative colormaps
Qualitative colormaps श्रेणीबद्ध डेटा का प्रतिनिधित्व करने के लिए आदर्श हैं जहाँ श्रेणियों का कोई क्रम नहीं होता। Qualitative colormaps का एक सामान्य उपयोग पाई चार्ट में होता है जहाँ प्रत्येक खंड एक अलग श्रेणी दर्शाता है जिसे आसानी से अलग पहचाना जा सके।
# Sample categorical data
categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
values = [20, 35, 25, 10, 10]
# Set the figure size
plt.figure(figsize=(8, 8))
# Define a qualitative colormap manually
colors = ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3'] # Manually selected color hex codes
# Create a pie chart
plt.pie(values, labels=categories, autopct='%1.1f%%', startangle=90, colors=colors)
# Equal aspect ratio ensures the pie is drawn as a circle
plt.axis('equal')
# Add a title
plt.title('Pie Chart with Qualitative Colormap')
# Display the chart
plt.show()

Matplotlib qualitative colormap का विज़ुअलाइज़ेशन उदाहरण। छवि: लेखक।
Rainbow colormaps
Rainbow colormaps जैसे hsv तब उपयोग किए जाते हैं जब विभिन्न hues की चौड़ी रेंज की आवश्यकता होती है।
# Generate cyclic data
x = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x)
# Create a plot with rainbow colormap
plt.scatter(x, y, c=x, cmap='hsv', s=50)
# Add a color bar
plt.colorbar(label='Phase')
# Add labels and title
plt.xlabel('Angle (radians)')
plt.ylabel('Sine Value')
plt.title('Cyclic Data Visualization using HSV Colormap')
# Display the plot
plt.show()

Matplotlib rainbow colormap का विज़ुअलाइज़ेशन उदाहरण। छवि: लेखक।
Perceptually uniform colormaps
inferno और plasma जैसे colormaps विभिन्न डिस्प्ले माध्यमों में बेहतर दृश्यता प्रदान करते हैं।
# Generate synthetic elevation data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2)) * 100 # Elevation data
# Plot the elevation data using the 'plasma' colormap
plt.contourf(X, Y, Z, cmap='plasma')
# Add a color bar
plt.colorbar(label='Elevation (m)')
# Add title and labels
plt.title('Topographic Map (Plasma Colormap)')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
# Display the plot
plt.show()

Matplotlib perceptually uniform colormap का विज़ुअलाइज़ेशन उदाहरण। छवि: लेखक।
Matplotlib Colormaps बनाना और संशोधित करना
हालाँकि Matplotlib में बिल्ट-इन colormaps की एक विस्तृत शृंखला है, कुछ स्थितियों में आप अपने रंगों को कस्टमाइज़ करना चाहेंगे। Matplotlib में कस्टम colormaps बनाने के लिए मुख्य टूल हैं ListedColormap और LinearSegmentedColormap।
ListedColormap के साथ कस्टम colormap बनाना
ListedColomap आपको विशिष्ट रंगों की सूची से colormap बनाने देता है। यह तरीका श्रेणीबद्ध डेटा में अलग-अलग श्रेणियों को विशिष्ट रंग सौंपने के लिए उपयोगी है।
from matplotlib.colors import ListedColormap
# Categorical data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 40, 35, 30]
# Custom color list for the colormap (e.g., shades of blue, green, red, and purple)
custom_colors = ['#1f77b4', '#2ca02c', '#d62728', '#9467bd']
# Create a ListedColormap using the custom colors
custom_cmap = ListedColormap(custom_colors)
# Set the figure size
plt.figure(figsize=(8, 6))
# Create a bar plot with the custom colormap
bars = plt.bar(categories, values, color=custom_cmap.colors)
# Add a title
plt.title('Bar Chart with Custom ListedColormap')
# Label the axes
plt.xlabel('Categories')
plt.ylabel('Values')
# Display the plot
plt.show()

Matplotlib: ListedColormap के साथ कस्टम colormap। छवि: लेखक।
LinearSegmentedColormap के साथ कस्टम colormap बनाना
LinearSegmentedColormap आपको ऐसे colormaps बनाने देता है जो रंगों के बीच सुचारु रूप से संक्रमण करते हैं। यह जानना उपयोगी है क्योंकि सतत डेटा अक्सर ग्रेडिएंट रंग परिवर्तनों की आवश्यकता रखता है। यहाँ, मैं अपने विज़ुअल के लिए अंतरालों की संख्या भी निर्दिष्ट कर रहा/रही हूँ।
from matplotlib.colors import LinearSegmentedColormap
# Create a grid of values for a 2D Gaussian function
x = np.linspace(-2, 2, 500)
y = np.linspace(-2, 2, 500)
X, Y = np.meshgrid(x, y)
Z = np.exp(- (X**2 + Y**2))
# Define a custom colormap that transitions from blue to white to red
colors = ['blue', 'white', 'red']
custom_cmap = LinearSegmentedColormap.from_list('blue_white_red', colors)
# Adjust the number of color classes (discrete levels)
# Specify the number of levels (e.g., 10 for 10 distinct color bands)
num_classes = 10
levels = np.linspace(Z.min(), Z.max(), num_classes)
# Set the figure size
plt.figure(figsize=(8, 6))
# Plot the 2D Gaussian function with the adjusted colormap
contour = plt.contourf(X, Y, Z, levels=levels, cmap=custom_cmap)
# Add a color bar to show the discrete color classes
plt.colorbar(contour, label='Gaussian Function Value')
# Add labels for the axes
plt.xlabel('X')
plt.ylabel('Y')
# Add a title
plt.title(f'2D Gaussian with {num_classes} Discrete Color Classes')
# Display the plot
plt.show()

Matplotlib: color classes की संख्या समायोजित करना। छवि: लेखक।
रंग रेंज और तीव्रता को कस्टमाइज़ करना
आप normalization में बदलाव करके या किसी मौजूदा colormap को स्लाइस कर के उपलब्ध रंगों के उपसमुच्चय का उपयोग करते हुए रंगों की तीव्रता या रेंज को नियंत्रित कर सकते हैं। नीचे के उदाहरण में, 16 से 40 डिग्री सेल्सियस के बीच तापमान दिखाने के लिए रंग रेंज को स्लाइस किया गया है।
# Set up the plot for the heatmap
plt.figure(figsize=(12, 8))
# Define a custom normalization for temperature range
norm = mcolors.Normalize(vmin=16, vmax=40)
# Create the heatmap with a sequential colormap
heatmap = plt.imshow(monthly_temperature_table, cmap='YlOrRd', aspect='auto', norm=norm)
# Add color bar to represent temperature
colorbar = plt.colorbar(heatmap, orientation='horizontal')
colorbar.set_label('Temperature (°C)', labelpad=10)
# Label the axes
plt.xlabel('Day of the Month')
plt.ylabel('Month')
# Set y-ticks to display month names instead of numbers
plt.yticks(ticks=np.arange(len(month_names)), labels=month_names)
# Add a title to the heatmap
plt.title('Monthly Temperature Heatmap (Sequential Colormap)')
# Display the plot
plt.grid(False)
plt.show()

Matplotlib: रंग रेंज और तीव्रता को कस्टमाइज़ करना। छवि: लेखक।
मौजूदा colormaps को संयोजित करना
आप कई colormaps को ब्लेंड करके अधिक जटिल विज़ुअलाइज़ेशन बनाने के लिए मौजूदा colormaps को भी संयोजित कर सकते हैं।
from matplotlib.colors import LinearSegmentedColormap, Normalize
# Create synthetic elevation data
x = np.linspace(-180, 180, 500) # Longitude
y = np.linspace(-90, 90, 250) # Latitude
X, Y = np.meshgrid(x, y)
Z = 5000 * np.sin(np.sqrt(X**2 + Y**2) * np.pi / 180) # Synthetic elevation data
# Define the base colormaps ('viridis' and 'cividis')
cmap1 = plt.get_cmap('viridis')
cmap2 = plt.get_cmap('cividis')
# Create a custom colormap by blending the two base colormaps
def blend_colormaps(cmap1, cmap2, blend_ratio=0.5):
"""Blend two colormaps together."""
c1 = cmap1(np.linspace(0, 1, 256))
c2 = cmap2(np.linspace(0, 1, 256))
blended_colors = (1 - blend_ratio) * c1 + blend_ratio * c2
return LinearSegmentedColormap.from_list('blended_cmap', blended_colors)
# Create the blended colormap
custom_cmap = blend_colormaps(cmap1, cmap2, blend_ratio=0.5)
# Normalize the data for visualization
norm = Normalize(vmin=-5000, vmax=5000)
# Set the figure size
plt.figure(figsize=(12, 6))
# Plot the synthetic elevation data with the blended colormap
contour = plt.contourf(X, Y, Z, levels=100, cmap=custom_cmap, norm=norm)
# Add a color bar to show the blended color mapping
plt.colorbar(contour, label='Elevation (meters)')
# Add labels for the axes
plt.xlabel('Longitude')
plt.ylabel('Latitude')
# Add a title
plt.title('Geographical Elevation Data with Blended Colormap (Viridis + Cividis)')
# Display the plot
plt.show()

Matplotlib: मौजूदा colormaps को संयोजित करना। छवि: लेखक।
_r suffix से colormaps को रिवर्स करना
आप Matplotlib में किसी भी colormap को उसके नाम में _r suffix जोड़कर रिवर्स कर सकते हैं।
# Set up the plot for the heatmap
plt.figure(figsize=(12, 8))
# Create a custom normalization for the color range
norm = mcolors.Normalize(vmin=16, vmax=40)
# Plot the heatmap using imshow with the sequential colormap
im = plt.imshow(monthly_temperature_table, cmap='YlOrRd_r', norm=norm, aspect='equal')
# Add a color bar with a horizontal orientation
cbar = plt.colorbar(im, orientation='horizontal', pad=0.1)
cbar.set_label('Temperature (°C)', labelpad=10)
# Add labels for the axes
plt.xlabel('Day of the Month')
plt.ylabel('Month')
# Set y-ticks to display month names instead of numbers
plt.yticks(ticks=np.arange(len(month_names)), labels=month_names)
# Add a title
plt.title('Temperature Heatmap (Sequential Colormap)')
# Display the plot
plt.show()

Matplotlib: _r suffix का उपयोग करके colormaps रिवर्स करना। छवि: लेखक।
Matplotlib Colormaps में आम त्रुटियों का समाधान
Matplotlib में colormaps के साथ काम करते समय, आपको ऐसी त्रुटियाँ मिल सकती हैं जो आपके कार्यप्रवाह को प्रभावित करती हैं। नीचे कुछ सामान्य त्रुटियाँ और उनका समाधान दिया गया है।
AttributeError: module 'matplotlib' has no attribute 'colormaps’
यह त्रुटि तब आती है जब आप matplotlib.colormaps तक पहुँचने के लिए Matplotlib 3.4.0 से पहले के संस्करणों का उपयोग करते हैं। Colormaps attribute Matplotlib 3.4.0 में पेश किया गया था। इस त्रुटि का समाधान करने के लिए पहले नीचे दिए गए कोड से अपना Matplotlib संस्करण जाँचें।
import matplotlib
print(matplotlib.__version__)
निम्नलिखित pip कमांड से अपग्रेड करें।
pip install --upgrade matplotlib
यदि आपको Matplotlib संस्करण अपग्रेड करने में दिक्कत आती है, तो colormaps तक पहुँचने के लिए नीचे दी गई विधि आज़माएँ।
plt.get_cmap('viridis') # instead of matplotlib.colormaps['viridis']
गलत रंग असाइनमेंट
जब आप अपेक्षित रेंज के बाहर का मान पास करते हैं या colormap गलत तरीके से लागू होता है, तो आपको गलत रंग असाइनमेंट का अनुभव हो सकता है। यदि colormap सतत डेटा के लिए है लेकिन इसे श्रेणीबद्ध डेटा पर लागू किया गया है या इसके विपरीत, तो भी गलत असाइनमेंट हो सकता है।
यह सुनिश्चित करने के लिए कि आपका डेटा अपेक्षित रेंज के भीतर है, हमेशा vmax और vmin पैरामीटर्स का उपयोग करके उसे normalize या scale करें।
import matplotlib.colors as mcolors
# Create a sample temperature dataset
data = np.random.rand(10, 10) * 100 # Adjust the range to match your data
# Create a color normalization instance to scale the data to the colormap's range
norm = mcolors.Normalize(vmin=0, vmax=100)
# Create a heatmap using the 'viridis' colormap and the specified normalization
plt.imshow(data, cmap='viridis', norm=norm, interpolation='nearest')
plt.colorbar()
plt.show()
और अधिक नियंत्रण के लिए, आप plotting फ़ंक्शंस में boundaries या norm पैरामीटर्स का उपयोग करके रंग सीमाएँ स्पष्ट रूप से परिभाषित कर सकते हैं।
दृश्य आर्टिफैक्ट्स और अप्रत्याशित परिणाम
डिस्क्रीट या लो-रेज़ोल्यूशन डेटा के साथ काम करते समय, आपको अनचाही रंग पट्टियाँ, अत्यधिक कंट्रास्ट, या रंग ब्लीडिंग का सामना करना पड़ सकता है। समस्या तब होती है जब आपका डेटा सीमित रेंज का हो लेकिन उसे continuous colormap के साथ प्रदर्शित किया जाए।
इस समस्या को हल करने के लिए, अधिक डेटा पॉइंट्स का उपयोग करें या ट्रांज़िशन को स्मूद करने के लिए रंग स्तरों की संख्या समायोजित करें।
# Increase the number of bins in the colormap
cmap = plt.get_cmap('viridis', 256)
plt.imshow(data, cmap=cmap)
उपयुक्त स्थानों पर discrete डेटा को संभालने के लिए आप colormap को निश्चित स्तरों में बाँट सकते हैं।
cmap = plt.get_cmap('inferno', 10) # Divide colormap into 10 discrete levels
plt.imshow(data, cmap=cmap)
आप डिस्प्ले सेटिंग्स भी जाँच सकते हैं और विशेषकर विज़ुअलाइज़ेशन सेव करते समय रेज़ोल्यूशन समायोजित कर सकते हैं ताकि लो-रेज़ोल्यूशन चार्ट से बचा जा सके
plt.savefig('plot.png', dpi=300) # Save the figure with a higher resolution
क्लिपिंग के कारण रंग विरूपण
जब डेटा में अपेक्षित रंग रेंज से बाहर के चरम मान होते हैं, तो रंग विरूपण होता है। इससे डेटा के हिस्से क्लिप हो जाते हैं और colormap में न्यूनतम या अधिकतम रंग पर असाइन हो जाते हैं।
रंग विरूपण से बचने के लिए, vmin और vmax का उपयोग करके रंग सीमाएँ अपने डेटा की रेंज से मिलाएँ, जिससे क्लिपिंग रोकी जा सके।
plt.imshow(data, cmap='plasma', vmin=0, vmax=100)
आप आउटलायर्स को सँभालने के लिए robust scaling भी उपयोग कर सकते हैं, जिसमें अत्यधिक मानों को छोड़कर कस्टम सीमाएँ सेट की जाती हैं।
import numpy as np
robust_vmin, robust_vmax = np.percentile(data, [2, 98]) # Use percentiles to remove extreme outliers
plt.imshow(data, cmap='inferno', vmin=robust_vmin, vmax=robust_vmax)
3D प्लॉट्स में colormap सही तरह प्रदर्शित न होना
यदि 3D सतह में डेटा मैपिंग में समस्या है, तो 3D विज़ुअलाइज़ेशन में colormaps सही नहीं दिख सकते। समाधान के लिए, सुनिश्चित करें कि z-अक्ष का डेटा colormap की अपेक्षित रेंज में normalize किया गया है। साथ ही, z-मूल्यों की पूरी रेंज कैप्चर करने के लिए उपयुक्त colormap सीमाएँ उपयोग करें।
from mpl_toolkits.mplot3d import Axes3D
# Generate sample data for a 3D surface plot
X, Y = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50))
Z = np.sin(np.sqrt(X**2 + Y**2))
# Create a 3D axes object
ax = plt.axes(projection='3d')
# Plot the surface using the 'viridis' colormap and adjusting the color limits
ax.plot_surface(X, Y, Z, cmap='viridis', vmin=np.min(Z), vmax=np.max(Z))
plt.show()
निष्कर्ष
सही colormap चुनना एक महत्वपूर्ण चरण है क्योंकि यह वास्तव में तय करता है कि उपयोगकर्ता आपके डेटा को कैसे देखते और समझते हैं। मेरे विचार में, डेटा विश्लेषण पर बहुत समय लगाकर अंतिम चरण—जहाँ आप अपने निष्कर्ष संप्रेषित करते हैं—में लापरवाही करना एक बड़ी गलती है।
खुशकिस्मती से, Matplotlib कई विकल्प प्रदान करता है ताकि आपकी विज़ुअल एक प्रभावी कहानी कहें। हमारे कोर्स Introduction to Data Visualization with Matplotlib में नामांकन करें और विशेषज्ञ बनें। साथ ही, यदि आप Python उपयोगकर्ता हैं जो विज़ुअलाइज़ेशन के लिए Seaborn भी उपयोग करते हैं (दोनों जानना अच्छा है), तो हमारा Quick Guide to Picking Colors in Seaborn पढ़ें और उसके बाद हमारा Introduction to Data Visualization with Seaborn कोर्स करें।
डेटा एनालिटिक्स, बिज़नेस इंटेलिजेंस और डेटा साइंस में व्यावहारिक अनुभव रखने वाला डेटा साइंस तकनीकी लेखक। मैं SQL, Python, Power BI, Databricks और डेटा इंजीनियरिंग पर व्यावहारिक, उद्योग-केंद्रित सामग्री लिखता/लिखती हूँ, जो वास्तविक दुनिया की एनालिटिक्स कार्य से जुड़ी होती है। मेरी लेखनी तकनीकी गहराई और व्यावसायिक प्रभाव के बीच पुल का काम करती है, जिससे पेशेवरों को डेटा को आत्मविश्वासपूर्ण निर्णयों में बदलने में मदद मिलती है.
Matplotlib Colormaps FAQs
Matplotlib में डिफ़ॉल्ट colormap क्या है?
Matplotlib में डिफ़ॉल्ट colormap viridis है।
मैं त्रुटि 'AttributeError: module 'matplotlib' has no attribute 'colormaps'?' कैसे ठीक करूँ?
त्रुटि AttributeError: module 'matplotlib' has no attribute 'colormaps' को ठीक करने के लिए, अपनी Matplotlib लाइब्रेरी को संस्करण 3.4.0 से नए संस्करण में अपग्रेड करें।
मैं colorblind उपयोगकर्ताओं के लिए colormap की सुगम्यता का परीक्षण कैसे कर सकता/सकती हूँ?
आप Coblis (Color Blindness Simulator) जैसे टूल्स या palettable जैसी थर्ड-पार्टी लाइब्रेरीज़ का उपयोग करके colorblind उपयोगकर्ताओं के लिए colormap की सुगम्यता का परीक्षण कर सकते हैं, जो colorblind दर्शकों के लिए colormaps का रूपों का सिमुलेशन करती हैं।
Perceptually uniform colormap क्या है?
Perceptually uniform colormap यह सुनिश्चित करता है कि डेटा में समान कदम रंग में समान धारणा-आधारित परिवर्तन उत्पन्न करें, जिससे व्याख्या सटीक हो।
क्या मैं Matplotlib में बाहरी लाइब्रेरीज़ के कस्टम colormaps का उपयोग कर सकता/सकती हूँ?
हाँ, palettable या cmocean जैसी बाहरी लाइब्रेरीज़ कस्टम colormaps प्रदान करती हैं जिन्हें Matplotlib के साथ विशेषीकृत विज़ुअलाइज़ेशन आवश्यकताओं के लिए एकीकृत किया जा सकता है।
