Debugging Visualizations
When charts go wrong, the data is usually trying to tell you something.
A blank chart, a jumbled line, an empty legend — these are the everyday frustrations of visualization. The good news: most visualization bugs fall into a small number of recognizable patterns. Learn the patterns and you'll stop being stuck.
The mindset: "the chart is honest about the data"
If your chart looks wrong, the chart is probably showing the data correctly — but the data isn't what you thought it was. The first move is rarely "fix the chart code." It's usually inspect the DataFrame.
A debugging session almost always starts like this:
Those four lines of inspection solve half of all visualization bugs.
Common failure modes
1. Wrong dtype on the axis
You expected a line chart of dates, but your "date" column is a string. Plotly will treat each value as a category and order them alphabetically. Fix:
2. Unsorted x in a line chart
Lines connect points in the order the DataFrame gives them. If
the data isn't sorted by x, you get a "scribble." Always
sort_values() before a line chart.
3. Long-vs-wide format confusion
Plotly Express expects long format: one row per observation,
columns for category/value/time. If you have a wide table
(columns are months, say), reshape with df.melt(...) first.
4. Empty chart after filtering
You filter the DataFrame and Plotly renders an empty canvas. Two likely causes:
- Your filter condition matches zero rows (typo? wrong comparison?).
- You filtered on a column that has
NaNvalues, which silently drop out of comparisons.
Check len(df_filtered) before plotting.
5. Missing values warping the chart
A single NaN in a line chart creates a gap. A single huge
outlier compresses every other point against the floor. Two
fixes:
df.dropna(subset=["y"])— drop missing rows for the columns you're plotting.- Inspect the y-range with
df["y"].describe()to spot outliers.
6. Log of zero or negative
px.scatter(..., log_y=True) will silently drop zero and negative
values (you can't take log of them). If your chart suddenly has
fewer points than your DataFrame, this is a likely culprit.
7. Too many categories crowding the legend
If color="city" has 200 cities, the legend becomes useless and
the colors become indistinguishable. Two fixes:
- Aggregate / group small categories into "Other."
- Use faceting instead of color when categories are many.
8. Mismatched data and figure types
Trying to make a line chart from a categorical x-axis usually indicates you meant a bar chart. Trying to make a scatter from two categorical columns usually indicates you meant a heatmap or grouped bar.
The debugging loop
When a chart looks wrong:
- Inspect the DataFrame.
head(),dtypes,shape,isna().sum(),describe(). - Check the column types. Numeric where you expect numeric? Datetime where you expect datetime?
- Sort the data if you're plotting a line.
- Filter and re-inspect before plotting.
- Simplify the chart — drop
color,facet,sizeand see if the basic shape is right. - Add encodings back one at a time.
This loop will resolve nearly every "my chart looks wrong" moment you'll encounter as a beginner.
A worked example
Check your understanding
Your line chart looks like a scribble — lines crossing back and forth instead of going left-to-right. The most likely cause is:
A bug in Plotly.
The wrong template.
The DataFrame is not sorted by the x-axis column. Lines connect points in DataFrame order, not in x-axis order.
You need more colors.
Plotly Express generally expects data in:
Wide format (one column per category).
Long format (one row per observation, with separate columns for the category, the value, and any grouping variable).
A NumPy array.
A nested dictionary.
You add log_y=True to a scatter plot and suddenly some points disappear. Why?
Plotly randomly samples points on log axes.
Log of zero or a negative number is undefined, so those points are silently dropped.
Log axes only show 100 points.
The simple_white template hides points.
When a chart looks wrong, the first debugging step should be:
Try a different chart type.
Try a different template.
Inspect the DataFrame: head(), dtypes, shape, isna().sum(). The chart is usually showing the data correctly; the data isn't what you think it is.
Restart the notebook.