Ravindra BagaleCourses & study guides मराठी Track your progress

29. Python in Excel

Excel

29.6 Charts with Matplotlib and seaborn in Python in Excel

Example first. Raja wants a horizontal bar chart of Amount by City, smallest at the bottom, and a histogram of delivery minutes. Both are a few lines of Python. The chart comes back as an image object in the cell; one right-click puts it over the grid where he can resize it next to his Excel table.

Python charts — in 30 seconds

What: Matplotlib (plt) and seaborn (sns) are imported for you. A plotting call in a Python cell returns the chart as an image object that you can preview, display over cells, or show inside the cell as an Excel value.

Why: Python charts give types Excel does not have built in (histograms with exact bins, pair plots, regression plots) and they are made from the same DataFrame, so they refresh with the data.

Tiny example: sns.histplot(df["Mins"], bins=[5, 10, 15, 20]) draws 3 bars: 2 orders took 5–9 minutes, 5 took 10–14, 3 took 15–20.

First look at the data (before → after):

Python charts: before and after tables, fictional FreshGo Maharashtra data

Expected result — Bars you should see (smallest to largest)

Bar Length (₹)
Sambhaji Nagar 195
Kolhapur 232
Nagpur 240
Nashik 360
Pune 544
Solapur 1299

About this data: Teaching sample for a fictional shop “FreshGo Mart”. Figures and names (e.g. Rohan Patil) are made up for this lesson on charts with matplotlib and seaborn in python in.

Before table and formula as text

Before — Amount by City (data for the chart)

City Amount
Sambhaji Nagar 195
Kolhapur 232
Nagpur 240
Nashik 360
… …

About this data: Demo table only — not real customer or company data. Built to practise charts with matplotlib and seaborn in python in.

Formula / click path

city.plot(kind="barh")  (pandas uses Matplotlib)
right-click › Display Plot over Cells  (Ctrl+Alt+Shift+C)

Do it step by step:

  1. In a cell after df, type the bar chart code from the box below and commit with Ctrl+Enter. The cell shows an image card.
  2. Select the card icon to preview the chart.
  3. Right-click the cell and choose Display Plot over Cells (or press Ctrl+Alt+Shift+C). A copy floats over the grid; drag its corners to resize it.
  4. To show the picture inside the cell instead, switch the output to Excel Value; make the cell bigger to make the picture bigger.
  5. In another cell, type the histogram code and commit. Check the bar heights: 2, 5 and 3.
  6. Add axis labels and a title in the same cell with plt.xlabel, plt.ylabel and plt.title.

1. Horizontal bar chart with pandas + Matplotlib

city = df.groupby("City")["Amount"].sum().sort_values()
city.plot(kind="barh")
plt.xlabel("Amount (Rs)")
plt.title("Amount by City (fictional data)")

sort_values() puts the smallest total first, and a horizontal bar chart draws the first item at the bottom, so Solapur (₹1,299) ends up as the longest bar at the top. Bar lengths: Sambhaji Nagar 195, Kolhapur 232, Nagpur 240, Nashik 360, Pune 544, Solapur 1299.

2. seaborn bar chart straight from the DataFrame

sns.barplot(data=df, x="City", y="Amount", estimator="sum", errorbar=None)
plt.xticks(rotation=30)
plt.title("Amount by City")

estimator="sum" makes each bar the total Amount of the city (seaborn's default would be the average), and errorbar=None removes the black error lines that seaborn adds by default. Bars appear in the order the cities first appear in the data: Pune, Nashik, Nagpur, Kolhapur, Solapur, Sambhaji Nagar.

3. Histogram of delivery minutes with exact bins

sns.histplot(df["Mins"], bins=[5, 10, 15, 20])
plt.title("Delivery minutes")
Bin (minutes) Orders Which orders
5 to under 10 2 BLK-1001, BLK-1006
10 to under 15 5 BLK-1002, AMN-1003, BLK-1004, AMN-1008, BLK-1009
15 to 20 3 AMN-1005, BLK-1007, BLK-1010

About this data: Made-up rows so you can copy the pattern for charts with matplotlib and seaborn in python in. Currencies may use ₹, £, € or $.

Expected output computed by running this exact code locally with pandas on the same fictional data (Python in Excel runs it in the Microsoft Cloud; numbers are the same, the look of the card may differ). The last bin includes its right edge (20); the others do not, which is how NumPy and seaborn define bins. Excel's own histogram chart and the FREQUENCY function use different edge rules, so always say which bins you used.

Getting the chart onto the sheet

Option How Best for
Preview only Select the card icon in the cell Quick look while you work
Display Plot over Cells Right-click › Display Plot over Cells, or Ctrl+Alt+Shift+C A chart you resize and place next to tables (the original stays in the cell)
Picture in the cell Python output menu › Excel Value (or Ctrl+Alt+Shift+M) A small chart per row or a compact dashboard; size follows the cell

About this data: Fictional worksheet used in class. Safe to edit; nothing here is live production data.

Python chart or Excel chart? If the chart must be formatted by colleagues who do not know Python, or must react to slicers, make an Excel chart from the spilled Excel values. Choose a Python chart when you need something Excel cannot draw easily (exact-bin histograms, box plots, pair plots, regression lines) or when the chart is part of an analysis already written in Python.

Indian-language labels. Microsoft's plotting page lists excel.FontPath.HINDI (Mangal font) for chart labels in Devanagari; without it non-Latin characters show as empty boxes. In this course we keep chart labels in English.

Microsoft sources (checked 7 October 2026): Create plots and charts with Python in Excel; Open-source libraries and Python in Excel; Python in Excel keyboard shortcuts.

Practice task (try it before you open the answer):

  1. Draw a bar chart of the number of orders per Platform.
  2. Say the bar heights.
  3. Display it over cells.
Answer
  1. df["Platform"].value_counts().plot(kind="bar") then plt.title("Orders by platform").
  2. FreshGo 7, MarketRush 3 (value_counts sorts largest first).
  3. Right-click the cell › Display Plot over Cells, or press Ctrl+Alt+Shift+C.

Ravindra Bagale's Tip

Put the numbers next to every Python chart, as a small Excel-value table. A picture cannot be checked; the table behind it can.

Worked example — charts with matplotlib and seaborn in python in

Why

Sana Sheikh in Pune and Liam Murphy abroad both need a clear, repeatable way to use charts with matplotlib and seaborn in python in on a CityBasket Retail sheet without breaking totals.

What

Charts With Matplotlib And Seaborn In Python In is the Excel idea this page teaches. We will walk one tiny example end to end.

How

  1. Open a blank sheet. In A1 type a short label for charts with matplotlib and seaborn in python in.
  2. Enter a small made-up value for Sana Sheikh using € where money appears.
  3. Add a second value for Liam Murphy using ₹ so you see mixed currencies stay numeric.
  4. Apply the method from this lesson step by step (menu or formula as shown above).
  5. Check the result still works inside SUM or a simple check cell.

Formula / action

Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Result: The demo cell shows the expected result for CityBasket Retail's sample row.
Meaning: Once charts with matplotlib and seaborn in python in is correct on one row, copy the same pattern down the column.

Practice tasks (10+)

Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.

1. Rebuild the smallest example of charts with matplotlib and seaborn in python in on a blank sheet for BrightLedger Finance.

Hint: Use only the steps on this page. Keep numbers numeric.

2. Change one input for Priya Deshmukh and confirm the result updates.

Hint: If the result is stuck, check absolute vs relative references.

3. Repeat the same steps with a £ or € value for Liam Murphy.

Hint: Formatting must not break SUM.

4. Write the result meaning in one plain sentence next to the cell.

Hint: Imagine explaining it to a new joiners.

5. Add a second row and fill the method down.

Hint: Double-click the fill handle if the pattern is consistent.

6. Break the example on purpose, then fix it using this lesson.

Hint: Common break: text instead of a number.

7. Apply charts with matplotlib and seaborn in python in to a 5-row made-up BrightLedger Finance table.

Hint: Keep column headers clear.

8. Check your answer with a simple total or COUNT.

Hint: A wrong total usually means a format or key mismatch.

9. Swap ₹ and $ on one column and re-check totals.

Hint: Stored values stay the same; only the display changes.

10. List two mistakes beginners make with charts with matplotlib and seaborn in python in.

Hint: Look back at the tip box on this page.

11. Teach the steps out loud in under one minute.

Hint: Why → What → How.

12. Save the sheet as BrightLedger_practice.xlsx.

Hint: Use a clear file name for class.

Loading practice tasks...

  1. len(df) (Easy): How many rows are in the Order ID column?
  2. df['Amount'].sum() (Easy): Add up the Amount column.
  3. df['Qty'].max() (Easy): What is the largest value in the Qty column?
  4. df['Amount'].mean() (Easy): Find the average Amount, rounded to 0 decimals.
  5. value_counts() for App (Medium): How many rows have Channel "App"?
  6. groupby('Channel').sum() for Store (Medium): Add up Amount where Channel is "Store".
  7. df['Amount'].median() (Medium): Find the median of the Amount column.
  8. df['Amount'].min() (Medium): What is the smallest value in the Amount column?
  9. df[df['Qty']>=5] (Medium): How many rows have Qty of 5 or more?
  10. std() of Amount (Hard): Find the sample standard deviation of Amount, rounded to 1 decimal.
  11. groupby mean for Website (Hard): Find the average Amount where Channel is "Website", rounded to 0 decimals.
  12. idxmax() of Amount (Hard): Which Customer has the largest Amount?