Seaborn/Matplotlib Date Axis barplot minor-major t

2019-05-07 06:53发布

I'm building a Seaborn barplot. The x-axis are dates, and the y-axis are integers.

I'd like to format major/minor ticks for the dates. I'd like Mondays' ticks to be bold and a different color (ie, "major ticks"), with the rest of the week less bold.

I have not been able to get major and minor tick formatting on the x-axis to work with Seaborn barplots. I'm stumped, and thus turning here for help.

I'm starting with the stackoverflow example that answered this question: Pandas timeseries plot setting x-axis major and minor ticks and labels

If I do a simple modification it to use a Seaborn barplot and I lose my X-axis ticks:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

import seaborn as sns

idx = pd.date_range('2011-05-01', '2011-07-01')
s = pd.Series(np.random.randn(len(idx)), index=idx)

###########################################
## Swap out these two lines of code:
#fig, ax = plt.subplots()
#ax.plot_date(idx.to_pydatetime(), s, 'v-')

## with this one
ax = sns.barplot(idx.to_pydatetime(), s)
###########################################

ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),
                                                interval=1))
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, which="minor")
ax.yaxis.grid()
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n\n%b\n%Y'))
plt.tight_layout()

## save the result to a png instead of plotting to screen:
myFigure = plt.gcf()
myFigure.autofmt_xdate()
myFigure.set_size_inches(11,3.8)

plt.title('Example Chart', loc='center')

plt.savefig('/tmp/chartexample.png', format='png', bbox_inches='tight')

I've tried a variety of approaches but something in Seaborn seems to be overriding or undoing any attempts at major and minor axis formatting that I've managed to cook up yet beyond some simple styling for all ticks when I use set_xticklabels().

I can sort of get formatting on just the major ticks by using MultipleLocator(), but I can't get any formatting on the minor ticks.

I've also experimented with myFigure.autofmt_xdate() to see if it would help, but it doesn't seem to like mixed major & minor ticks on the same axis either.

0条回答
登录 后发表回答