Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / Customizing Plots
interview

Q1. Scenario: Customize a line plot with linestyle dashed, linewidth 2, marker=''o'', markersize 8, and color=''green''.
plt.plot(x, y, linestyle=''--'', linewidth=2, marker=''o'', markersize=8, color=''green''). Also shorthand: ''g--o'' for green dashed with circles. Customization improves readability.

Q2. Scenario: Add grid, set xlim and ylim, and add text annotation at a specific point.
plt.grid(True, linestyle='':'', alpha=0.7); plt.xlim(0,10); plt.ylim(0,100); plt.text(2, 80, ''Peak'', fontsize=12, bbox=dict(facecolor=''yellow'')).

Q3. Scenario: Create a figure with a title, axis labels, and a legend outside the plot area using bbox_to_anchor.
plt.plot(x, y1, label=''Line1''); plt.plot(x, y2, label=''Line2''); plt.legend(bbox_to_anchor=(1.05, 1), loc=''upper left''); plt.tight_layout(). Places legend outside.

Q4. Scenario: Use different tick formats, e.g., show x-axis as dates formatted YYYY-MM-DD.
import matplotlib.dates as mdates; plt.plot_date(dates, values); plt.gca().xaxis.set_major_formatter(mdates.DateFormatter(''%Y-%m-%d'')); plt.gcf().autofmt_xdate().

Q5. Scenario: Annotate a bar chart with the bar values on top of each bar.
bars = plt.bar(categories, values); for bar in bars: height = bar.get_height(); plt.text(bar.get_x() + bar.get_width()/2., height, f''{height}'', ha=''center'', va=''bottom'').