0

I have a dataset with 60 dates and values, which I'd like to visualise as a scatterplot.

The space between the individual however is pretty small, resulting in the dates to overlap. I tried rotating them but this didn't really solve my problem. I don't want to change the fontsize either, nor can I omit some labels. I basically want to increase the distance between each label, while keeping them aligned with the actual ticks (in other words: I want to increase the distance between the ticks)

I tried doing this, but it didn't work for me.

day = df["day"].tolist() #a series of dates YYYY-MM-DD (not necessarily consecutive)
median = df[" median"].tolist() #some integers

x = range(len(day))

fig, ax = plt.subplots()
ax.scatter(x,median, color='blue')
plt.xticks(x, day, rotation=45, fontsize=10)
ax.set_ylim(ymin=0)

Example

Community
  • 1
  • 1
Readler
  • 149
  • 1
  • 10
  • Can you show what it looks like now? – Neal Titus Thomas Jun 04 '19 at 10:32
  • @NealTitusThomas done! – Readler Jun 04 '19 at 10:39
  • Possible duplicate of [How to change spacing between ticks in matplotlib?](https://stackoverflow.com/questions/44863375/how-to-change-spacing-between-ticks-in-matplotlib) – Zaraki Kenpachi Jun 04 '19 at 10:41
  • The space between tick labels is decided by the space between ticks on the axes. The only way to do what you want is to make the axes larger. – Neal Titus Thomas Jun 04 '19 at 10:43
  • are these "dates" being stored as `datetime64`s or `str`ings? at a guess they're strings so matplotlib doesn't know what to do with them. check out [`to_datetime`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html) – Sam Mason Jun 04 '19 at 10:47

1 Answers1

0

You probably want to increase the width of the plot to fit all the labels nicely without changing the font size and the number of ticks.

Try playing with plt.figure(figsize=(width, height)) in order to have a width that fit your desires.

More info here: https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.figure.html

Also, try without subplots:

plt.figure(figsize=(width, height))
plt.scatter(x, median, c='blue')
plt.xticks(x, day, rotation=45, fontsize=10)
plt.ylim(bottom=0)
plt.show()
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • I already tried increasing the width, but this results in a blank image for some reason. – Readler Jun 04 '19 at 10:40
  • That is another problem. Try without subplots as suggested in the last version of the answer. Let me know how it goes, please. – alec_djinn Jun 04 '19 at 10:53
  • Also, if you can print out `day` and `median` I can try to plot it on my machine and show you the result. – alec_djinn Jun 04 '19 at 10:54