Python Data Science Jobs & Interviews
20.7K subscribers
196 photos
4 videos
26 files
339 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
⁉️ Interview question
What happens when you call `plt.plot()` without specifying a figure or axes, and then immediately call `plt.show()`?

The function `plt.plot()` automatically creates a new figure and axes if none exist, and `plt.show()` displays the current figure. However, if multiple plots are created without clearing the figure, they may overlap or appear in unexpected orders due to matplotlib's internal state management. This behavior can lead to confusion, especially when working with loops or subplots.

#️⃣ tags: #matplotlib #python #datavisualization #plotting #beginner #codingchallenge

By: @DataScienceQ 🚀
⁉️ Interview question
How does `plt.subplot()` differ from `plt.subplots()` when creating a grid of plots?

`plt.subplot()` creates a single subplot in a grid by specifying row and column indices, requiring separate calls for each subplot. In contrast, `plt.subplots()` creates the entire grid at once, returning both the figure and an array of axes objects, making it more efficient for managing multiple subplots. However, using `plt.subplot()` can lead to overlapping or misaligned plots if not carefully managed, especially when adding elements like titles or labels.

#️⃣ tags: #matplotlib #python #plotting #subplots #datavisualization #beginner #codingchallenge

By: @DataScienceQ 🚀
#matplotlib #python #programming #question #visualization #intermediate

Write a Python program using matplotlib to perform the following tasks:

1. Generate two arrays: x from 0 to 10 with 100 points, and y = sin(x) + 0.5 * cos(2x).
2. Create a figure with two subplots arranged vertically.
3. In the first subplot, plot y vs x as a line graph with red color and marker 'o'.
4. In the second subplot, create a histogram of the y values with 20 bins.
5. Add titles, labels, and grid to both subplots.
6. Adjust the layout and save the figure as 'output_plot.png'.

import numpy as np
import matplotlib.pyplot as plt

# 1. Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x) + 0.5 * np.cos(2 * x)

# 2. Create figure with two subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))

# 3. First subplot - line plot
ax1.plot(x, y, color='red', marker='o', linestyle='-', linewidth=2)
ax1.set_title('sin(x) + 0.5*cos(2x)')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.grid(True)

# 4. Second subplot - histogram
ax2.hist(y, bins=20, color='blue', alpha=0.7)
ax2.set_title('Histogram of y values')
ax2.set_xlabel('y')
ax2.set_ylabel('Frequency')
ax2.grid(True)

# 5. Adjust layout
plt.tight_layout()

# 6. Save the figure
plt.savefig('output_plot.png')

print("Plot saved as 'output_plot.png'")

Note: This code generates a sine wave with an added cosine component, creates a line plot and histogram of the data in separate subplots, adds appropriate labels and grids, and saves the resulting visualization.

By: @DataScienceQ 🚀
🧠 Quiz: Which submodule of Matplotlib is commonly imported with the alias plt to create plots and visualizations?

A) matplotlib.animation
B) matplotlib.pyplot
C) matplotlib.widgets
D) matplotlib.cm

Correct answer: B

Explanation: matplotlib.pyplot is the most widely used module in Matplotlib, providing a convenient, MATLAB-like interface for creating a variety of plots and charts. It's standard practice to import it as import matplotlib.pyplot as plt.

#Matplotlib #Python #DataVisualization

━━━━━━━━━━━━━━━
By: @DataScienceQ
2🔥1