2

In the following code snippet:

import numpy as np
import pandas as pd
import pandas.rpy.common as com
import matplotlib.pyplot as plt
mtcars = com.load_data("mtcars")
df = mtcars.groupby(["cyl"]).apply(lambda x: pd.Series([x["cyl"].count(), np.mean(x["wt"])], index=["n", "wt"])).reset_index()
plt.plot(df["n"], range(len(df["cyl"])), "o")
plt.yticks(range(len(df["cyl"])), df["cyl"])
plt.show()

This code outputs the dot plot graph, but the result looks quite awful, since both the xticks and yticks don't have enough space, that it's quite difficult to notice both 4 and 8 of the cyl variable output its values in the graph.

So how can I plot it with enough space in advance, much like you can do it without any hassles in R/ggplot2?

For your information, both of this code and this doesn't work in my case. Anyone knows the reason? And do I have to bother to creating such subplots in the first place? Is it impossible to automatically adjust the ticks with response to the input values?

Community
  • 1
  • 1
Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • I usually adjust the ticks based on my input size, makes life a bit easier. – Raiyan Nov 13 '13 at 06:01
  • Can you add an image of the problem? (For what it's worth, the tick spacing _is_ automatically adjusted according to the input values. You've set them manually to something else, though.) – Joe Kington Nov 13 '13 at 16:09
  • Or am I misunderstanding what you're asking? Are you asking for more room around the edges of the plot? If you want to mimic R's behavior, use `ax.margins(0.04)`, which will add 4% padding around the data limits before autoscaling. – Joe Kington Nov 13 '13 at 16:19

1 Answers1

3

I can't quite tell what you're asking...

Are you asking why the ticks aren't automatically positioned or are you asking how to add "padding" around the inside edges of the plot?

If it's the former, it's because you've manually set the tick locations with yticks. This overrides the automatic tick locator.

If it's the latter, use ax.margins(some_percentage) (where some_percentage is between 0 and 1, e.g. 0.05 is 5%) to add "padding" to the data limits before they're autoscaled.

As an example of the latter, by default, the data limits can be autoscaled such that a point can lie on the boundaries of the plot. E.g.:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10), 'ro')
plt.show()

enter image description here

If you want to avoid this, use ax.margins (or equivalently, plt.margins) to specify a percentage of padding to be added to the data limits before autoscaling takes place.

E.g.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10), 'ro')
ax.margins(0.04) # 4% padding, similar to R.
plt.show()

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • Sorry for the confusion, I wanted to know both actually. Thanks a lot for your terrific clarification. – Blaszard Nov 15 '13 at 10:53