4

By chance, is there a way to rotate the xticks in the graphic below (just to make it a bit more readable)? The usual sns.xticks() doesn't work in the new seaborn.objects development (which is amazing!)

tcap.\
    assign(date_time2 = tcap['date_time'].dt.date).\
    groupby(['date_time2', 'person']).\
    agg(counts = ('person', 'count')).\
    reset_index().\
    pipe(so.Plot, x = "date_time2", y = "counts", color = "person").\
            add(so.Line(marker="o", edgecolor="w")).\
            label(x = "Date", y = "# of messages",
                  color = str.capitalize,
                  title = "Plot 2: Volume of messages by person, by day").\
            scale(color=so.Nominal(order=["lorne_a_20014", "kayla_princess94"])).\
            show()

enter image description here

In addition, my x-axis is categorical and this warning: Using categorical units to plot a list of strings that are all parsable as floats or dates. If these strings should be plotted as numbers, cast to the appropriate data type before plotting. appears. I tried using:

import warnings
warnings.filterwarnings("ignore",category=UserWarning)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Robert Chestnutt
  • 302
  • 3
  • 13

1 Answers1

4

This can be done by creating an Axis object, rotating the axes there, and then using the so.Plot().on() method to apply the rotated-axis labels. Note this will not work if you also plan to add facets (I found your question while coming to ask about how to combine this with facets).

import seaborn.objects as so
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'a':[1,2,3],
                   'b':[4,5,6]})

fig, ax = plt.subplots()
ax.xaxis.set_tick_params(rotation=90)

(so.Plot(df, x = 'a', y = 'b')
 .add(so.Line())
 .on(ax))

Line graph with rotated x-axis labels

NickCHK
  • 1,093
  • 7
  • 17