Too Many Labels on Axis

Too many factors on x axis

You can flip the values.

ggplot(data.frame(x=factor(trunc(runif(10000, 0, 100)), ordered=T)), aes(x=x)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_histogram()

flip <- ggplot(data.frame(x=factor(trunc(runif(10000, 0, 100)), ordered=T)), aes(x=x)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_histogram()

If it's still too dense for your taste, you can set manual breaks. In this case, I use five.

prune <- ggplot(data.frame(x=factor(trunc(runif(10000, 0, 100)), ordered=T)), aes(x=x)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_discrete(breaks = seq(0, 100, by = 5)) +
geom_histogram()

library(gridExtra)
grid.arrange(flip, prune)

Sample Image

Too many labels on axis

Your column value is likely a factor, when it should be a numeric. This causes each categorical value of value to be given its own entry on the y-axis, thus producing the effect you've noticed.

You should coerce it to be a numeric

data$value <- as.numeric(as.character(data$value))

Note that there is probably a good reason it has been interpreted as a factor and not a numeric, possibly because it has some entries that are not pure numeric values (maybe 1,000 or 1000 m or some other character entry among the numbers). The consequence of the coercion may be a loss of information, so be warned or cleanse the data thoroughly.

Also, you appear to have the same problem on the x-axis.

X-axis tick labels are too dense when drawing plots with matplotlib

A quick dirty solution would be the following:

ax.set_xticks(ax.get_xticks()[::2])

This would only display every second xtick. If you wanted to only display every n-th tick you would use

ax.set_xticks(ax.get_xticks()[::n])

If you don't have a handle on ax you can get one as ax = plt.gca().

Alternatively, you could specify the number of xticks to use with:

plt.locator_params(axis='x', nbins=10)

Choosing how many x axis labels display on an altair chart in python

You have declared that your x data is type O, meaning ordinal, i.e. ordered categories. This says that you want one distinct x bin for each unique value in your dataset. If you want fewer ordinal x bins, you should use a dataset with fewer unique values.

Alternatively, if you don't want each unique x value to have its own label, you can use the quantitative data type (i.e. x=alt.X('norm:Q')), or perhaps bin your data x=alt.X('norm:O', bin=True). Be sure to bin your color encoding as well if you use the latter.

Garbled x-axis labels in matplotlib subplots

Suggestions:

  1. Increase the height of the figure.
    fig, axes = plt.subplots(14, 4, figsize = (12,20), sharex = True)
  2. Rotate all the labels:
    fig.autofmt_xdate(rotation=90)
  3. Use tight_layout at the end instead of subplots_adjust:
    fig.tight_layout()

Sample Image



Related Topics



Leave a reply



Submit