I have a pandas dataframe al_df
that contains the population of Alabama from a recent US census. I created a cumulative function that I plot using seaborn
, resulting in this chart:
The code that relates to the plotting is this:
figure(num=None, figsize=(20, 10))
plt.title('Cumulative Distribution Function for ALABAMA population')
plt.xlabel('City')
plt.ylabel('Percentage')
#sns.set_style("whitegrid", {"ytick.major.size": "0.1",})
plt.plot(al_df.pop_cum_perc)
My questions are: 1) How can I change the ticks, so the yaxis shows a grid line every 0.1 units instead of the default 0.2 shown? 2) How can I change the x axis to show the actual names of the city, plotted vertically, instead of the "rank" of the city (from the Pandas index)? (there are over 300 names, so they are not going to fit well horizontally).
After some research, and not been able to find a "native" Seaborn solution, I came up with the code below, partially based on @Pablo Reyes and @CT Zhu suggestions, and using matplotlib functions:
The solution introduced a new element "labels" which I had to specify before the plot, as a new Python list created from my Pandas dataframe:
Producing the following chart:
This requires some tweaking, since specifying a display of every city in the pandas data frame, like this:
Produces a chart impossible to read (displaying only x axis):
For question 1) ,add:
Question 2), I found this in the matplotlib gallery: ticks_and_spines example code
The
matplotlib
way would be to useMutlipLocator
. The second one is also straight forward