Reputation: 827
Below is my multi indexed dataframe
travel_spending
state ethinicity
CA Asian 233.404580
MA Asian 748.117647
NY Asian 350.880000
CA Black 146.898148
MA Black 99.849057
NY Black 125.206897
CA Chinese 387.398601
MA Chinese 119.636364
NY Chinese 263.245283
CA Hispanic 131.156484
MA Hispanic 200.220859
NY Hispanic 175.738589
CA American Indian 36.500000
MA American Indian 67.500000
NY American Indian 81.800000
CA Japanese 365.029703
MA Japanese 28.666667
NY Japanese 241.500000
CA Other 257.953356
MA Other 208.178174
NY Other 255.144436
CA Portuguese 26.000000
MA Portuguese 22.000000
CA White 222.322485
MA White 167.293194
NY White 140.080838
I am able to generate subplots from these very easily:
travel_df_new.unstack(level=0).plot(kind='bar', subplots=True)
I want to get rid of the extra text that is inside the graph as it is redundant and also present outside it travel_spending,CA travel_spending MA,travel_spending, NY
ALso, I want to make the font of x axis(Ethinicity) more legible and make it more darker.
Are these things possible with matplotlib graphs?
Upvotes: 0
Views: 148
Reputation: 4855
You can get rid of the legend by passing legend=False
as a parameter to the .plot()
method of the dataframe:
travel_df_new.unstack(level=0).plot(kind='bar', subplots=True, legend=False)
(Note: it looks to me like the OP might be using the
ggplot
style with matplotlib, but this isn't specified in the question. Based on this, I'm using theggplot
style for this example.)import matplotlib matplotlib.style.use('ggplot')
One option is to set the tick_params for the current axis immediately after the plot is created. You can retrieve the current axis with plt.gca()
. Specify the appearance of the axis ticks by calling the tick_params()
method for the axis object. The example below shows setting the labelsize and the labelcolor for the x-axis only.
import matplotlib.pyplot as plt
travel_df_new.unstack(level=0).plot(kind='bar', subplots=True, legend=False)
plt.gca().tick_params(axis='x', labelsize='x-large', labelcolor='k')
The method above will only apply to the current plot, but you can also customize the appearance of elements in all matplotlib figures by setting values in the rcParams
dictionary. For example, this changes the color and size of the xticks:
import matplotlib.pyplot as plt
plt.rcParams['xtick.color']='k'
plt.rcParams['xtick.labelsize']='x-large'
travel_df_new.unstack(level=0).plot(kind='bar', subplots=True, legend=False)
These settings produce the following graph:
It looks like the ability to set the labelrotation
parameter will be added to tick_params()
in a future version of matplotlib. It shows up as an option in the latest documentation, but it isn't available in version 2.0.
In current versions of matplotlib (<= 2.0.x), use plt.setp()
to set the rotation.
travel_df_new.unstack(level=0).plot(kind='bar', subplots=True, legend=False)
plt.setp(plt.xticks()[-1], size='x-large', color='k', rotation=45, ha='right')
This call can also set the color
and the size
of the labels, so you can cover all of the tick formatting with this one call. The ha
parameter controls the horizontal alignment of the rotated labels and accepts the values 'left', 'center', and 'right'.
Upvotes: 1