I have a long bar chart with lots of bars and I wanna improve its reability from axis to the bars.
Suppose I have the following graph:
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(1,-1,20)
x = np.arange(0,20)
labels = [f'Test {i}' for i in x]
fig, ax = plt.subplots(figsize=(12,8))
sns.barplot(y = y, x = x, ax=ax )
ax.set_xticklabels(labels, rotation=90)
which provides me the following:
All I know is how to change the label position globally across the chart. How can I change the axis layout to be cantered in the middle and change its label position based on a condition (in this case, being higher or lower than 0)? What I want to achieve is:
Thanks in advance =)
You could remove the existing x-ticks and place texts manually:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
y = np.linspace(1,-1,20)
x = np.arange(0,20)
labels = [f'Test {i}' for i in x]
fig, ax = plt.subplots(figsize=(12,8))
sns.barplot(y = y, x = x, ax=ax )
ax.set_xticks([]) # remove existing ticks
for i, (label, height) in enumerate(zip(labels, y)):
ax.text(i, 0, ' '+ label+' ', rotation=90, ha='center', va='top' if height>0 else 'bottom' )
ax.axhline(0, color='black') # draw a new x-axis
for spine in ['top', 'right', 'bottom']:
ax.spines[spine].set_visible(False) # optionally hide spines
plt.show()
Here is another approach, I'm not sure whether it is "more pythonic".
move the existing xaxis to y=0
set the tick marks in both directions
put the ticks behind the bars
prepend some spaces to the labels to move them away from the axis
realign the tick labels depending on the bar value
fig, ax = plt.subplots(figsize=(12, 8))
sns.barplot(y=y, x=x, ax=ax)
ax.spines['bottom'].set_position('zero')
for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)
ax.set_xticklabels([' ' + label for label in labels], rotation=90)
for tick, height in zip(ax.get_xticklabels(), y):
tick.set_va('top' if height > 0 else 'bottom')
ax.tick_params(axis='x', direction='inout')
ax.set_axisbelow(True) # ticks behind the bars
plt.show()
Related
Can someone please help me plot x axis labels in percentages given the following code of my horizontal bar chart?
Finding it difficult to find as I want a more simplistic chart without x axis labels and ticks.
[Horizontal Bar Chart][1]
# Plot the figure size
plt.figure(figsize= (8,6))
# New variable and plot the question of the data frame in a normalized in a horizontal bar chat.
ax1 = df[q1].value_counts(normalize=True).sort_values().plot(kind="barh", color='#fd6a02', width=0.75, zorder=2)
# Draw vague vertical axis lines and set lines to the back of the order
vals = ax1.get_xticks()
for tick in vals:
ax1.axvline(x=tick, linestyle='dashed', alpha=0.4, color = '#d3d3d3', zorder=1)
# Tot3als to produce a composition ratio
total_percent = df[q1].value_counts(normalize=True) *100
# Remove borders
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.spines['left'].set_visible(False)
ax1.spines['bottom'].set_visible(False)
# Set the title of the graph inline with the Y axis labels.
ax1.set_title(q1, weight='bold', size=14, loc = 'left', pad=20, x = -0.16)
# ax.text(x,y,text,color)
for i,val in enumerate(total):
ax1.text(val - 1.5, i, str("{:.2%}".format(total_percent), color="w", fontsize=10, zorder=3)
# Create axis labels
plt.xlabel("Ratio of Responses", labelpad=20, weight='bold', size=12)
Each time I get a EOF error. Can someone help?
It's not based on your code, but I'll customize the answer from the official reference.
The point is achieved with ax.text(), which is a looping process.
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
# Totals to produce a composition ratio
total = sum(performance)
# ax.text(x,y,text,color)
for i,val in enumerate(performance):
ax.text(val - 1.5, i, str("{:.2%}".format(val/total)), color="w", fontsize=10)
plt.show()
I got a little problem and google couldn't really help me out. Here's my code:
from matplotlib import pyplot as plt
import numpy as np
job_r = list(ct_t.JobRole.unique())
att_y = ct_t[ct_t['Attrition']=='Yes']['Percentage'].values
att_n = ct_t[ct_t['Attrition']=='No']['Percentage'].values
# Sort by number of sales staff
idx = att_n.argsort()
job_r, att_y, att_n = [np.take(x, idx) for x in [job_r, att_y, att_n]]
y = np.arange(att_y.size)
fig, axes = plt.subplots(ncols=2, sharey=True, figsize=[8,8])
axes[0].barh(y, att_n, align='center', color='#43e653', zorder=10)
axes[0].set(title='NO')
axes[1].barh(y, att_y, align='center', color='#ed1c3c', zorder=10)
axes[1].set(title='YES')
axes[0].invert_xaxis()
axes[0].set(yticks=y, yticklabels=job_r)
axes[0].yaxis.tick_right()
for ax in axes.flat:
ax.margins(0.03)
ax.grid(True)
fig.tight_layout()
fig.subplots_adjust(wspace=0.7)
plt.show()
My current output:
Is there any way to center the shared y labels in the middle between those two subplots?
Can I increase the x-axis up to 1.0 ? Each time I do something like: axes[1].set_xlabels(1.0) my whole plot turns upside down.
Here is a way to achieve the desired plot.
Extending the x limits to 1 is easier if done before inverting the x-axis. The code to center and reposition the labels come from this post.
import matplotlib.pyplot as plt
import matplotlib.transforms
import numpy as np
# first create some test data compatible with the question's data
job_r = ["".join(np.repeat(letter, np.random.randint(4, 15))) for letter in 'ABCDEFG']
att_y = np.random.uniform(0.5, 0.9, len(job_r))
att_n = 1 - att_y
# Sort by number of sales staff
idx = att_n.argsort()
job_r, att_y, att_n = [np.take(x, idx) for x in [job_r, att_y, att_n]]
y = np.arange(att_y.size)
fig, axes = plt.subplots(ncols=2, sharey=True, figsize=[8, 8])
axes[0].barh(y, att_n, align='center', color='#43e653', zorder=10)
axes[0].set(title='NO')
axes[1].barh(y, att_y, align='center', color='#ed1c3c', zorder=10)
axes[1].set(title='YES')
axes[1].set_xlim(xmax=1)
axes[0].set(yticks=y, yticklabels=job_r)
axes[0].yaxis.tick_right()
axes[0].set_xlim(xmax=1)
axes[0].invert_xaxis()
for ax in axes:
ax.margins(0.03)
ax.grid(True)
fig.tight_layout()
fig.subplots_adjust(wspace=0.7)
plt.setp(axes[0].yaxis.get_majorticklabels(), ha='center')
# Create offset transform by some points in x direction
dx = 60 / 72.
dy = 0 / 72.
offset = matplotlib.transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)
# apply offset transform to all y ticklabels.
for label in axes[0].yaxis.get_majorticklabels():
label.set_transform(label.get_transform() + offset)
plt.show()
I need to rotate the 2nd y-axis ticklabel and add a label for this axis as well in the figure below
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms
fig, ax1 = plt.subplots(constrained_layout=True)
x = [61,62,62,59,62,59,62,63,61,60,103,104,109,105,109,105,109,111,110,107]
y = [62,62,62,62,60,60,62,62,62,63,106,107,106,106,105,105,105,106,107,108]
ax1.plot(x,y,'b.')
x2 = [2.2,3.4,4.3,5.1,5.5,5.7]
y2 = [2.3,2.8,3.2,3.9,4.5,5.9]
ax2 = ax1.twinx().twiny()
ax2.tick_params(axis="y",labelrotation=90,direction='out',length=6, width=2, colors='r',grid_color='r', grid_alpha=0.5) #called tick_params before the plot and didn't work
ax2.plot(x2,y2,'r.')
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
ax2.set_yticklabels(['Label1', 'Label2', 'Label3'], rotation=90) #y ticklabels is not rotating
ax2.set_xlabel('abc', rotation=0, fontsize=20, labelpad=20)
ax2.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20) #y label is not wroking
plt.yticks(rotation=90)
line = mlines.Line2D([0, 1], [0, 1], color='red')
transform = ax2.transAxes
line.set_transform(transform)
ax2.add_line(line)
plt.show()
This code produced the figure below
The problem is ax2.set_yticklabels and ax2.set_ylabel don't work.
I want to add a label to 2nd y-axis and rotate the tick label for that axis. Also, how to control the position of the tick mark at these axes, I want it to be at the same position of tick marks of 1st y-axis and 1st x-axis. So Label1 will shift up and 0 will shift right
Thanks
When you are instancing ax2 = ax1.twinx().twiny(), you can no longer modify the y axis. Instead, create two axes and modify accordingly. Modified code and the result is below.
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms
fig, ax1 = plt.subplots(constrained_layout=True)
x = [61,62,62,59,62,59,62,63,61,60,103,104,109,105,109,105,109,111,110,107]
y = [62,62,62,62,60,60,62,62,62,63,106,107,106,106,105,105,105,106,107,108]
ax1.plot(x,y,'b.')
x2 = [2.2,3.4,4.3,5.1,5.5,5.7]
y2 = [2.3,2.8,3.2,3.9,4.5,5.9]
ax2 = ax1.twinx() # ax2 handles y
ax3 = ax2.twiny() # ax3 handles x
ax3.plot(x2,y2,'r.')
ax3.set_xlim(0,10)
ax2.set_ylim(0,10)
ax3.set_xlabel('abc', rotation=0, fontsize=20, labelpad=20)
ax2.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20)
ax2.tick_params(axis="y",labelrotation=90,direction='out',length=6, width=2, colors='r',grid_color='r', grid_alpha=0.5)
ax2.set_yticklabels(['Label1', 'Label2', 'Label3'], rotation=-90)
plt.yticks(rotation=90)
line = mlines.Line2D([0, 1], [0, 1], color='red')
transform = ax2.transAxes
line.set_transform(transform)
ax2.add_line(line)
plt.show()
The resulting graph has all the y label/tick modifications.
I'm trying to create a horizontal bar chart, with dual x axes. The 2 axes are very different in scale, 1 set goes from something like -5 to 15 (positive and negative value), the other set is more like 100 to 500 (all positive values).
When I plot this, I'd like to align the 2 axes so zero shows at the same position, and only the negative values are to the left of this. Currently the set with all positive values starts at the far left, and the set with positive and negative starts in the middle of the overall plot.
I found the align_yaxis example, but I'm struggling to align the x axes.
Matplotlib bar charts: Aligning two different y axes to zero
Here is an example of what I'm working on with simple test data. Any ideas/suggestions? thanks
import pandas as pd
import matplotlib.pyplot as plt
d = {'col1':['Test 1','Test 2','Test 3','Test 4'],'col 2':[1.4,-3,1.3,5],'Col3':[900,750,878,920]}
df = pd.DataFrame(data=d)
fig = plt.figure() # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twiny() # Create another axes that shares the same y-axis as ax.
width = 0.4
df['col 2'].plot(kind='barh', color='darkblue', ax=ax, width=width, position=1,fontsize =4, figsize=(3.0, 5.0))
df['Col3'].plot(kind='barh', color='orange', ax=ax2, width=width, position=0, fontsize =4, figsize=(3.0, 5.0))
ax.set_yticklabels(df.col1)
ax.set_xlabel('Positive and Neg',color='darkblue')
ax2.set_xlabel('Positive Only',color='orange')
ax.invert_yaxis()
plt.show()
I followed the link from a question and eventually ended up at this answer : https://stackoverflow.com/a/10482477/5907969
The answer has a function to align the y-axes and I have modified the same to align x-axes as follows:
def align_xaxis(ax1, v1, ax2, v2):
"""adjust ax2 xlimit so that v2 in ax2 is aligned to v1 in ax1"""
x1, _ = ax1.transData.transform((v1, 0))
x2, _ = ax2.transData.transform((v2, 0))
inv = ax2.transData.inverted()
dx, _ = inv.transform((0, 0)) - inv.transform((x1-x2, 0))
minx, maxx = ax2.get_xlim()
ax2.set_xlim(minx+dx, maxx+dx)
And then use it within the code as follows:
import pandas as pd
import matplotlib.pyplot as plt
d = {'col1':['Test 1','Test 2','Test 3','Test 4'],'col 2' [1.4,-3,1.3,5],'Col3':[900,750,878,920]}
df = pd.DataFrame(data=d)
fig = plt.figure() # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twiny() # Create another axes that shares the same y-axis as ax.
width = 0.4
df['col 2'].plot(kind='barh', color='darkblue', ax=ax, width=width, position=1,fontsize =4, figsize=(3.0, 5.0))
df['Col3'].plot(kind='barh', color='orange', ax=ax2, width=width, position=0, fontsize =4, figsize=(3.0, 5.0))
ax.set_yticklabels(df.col1)
ax.set_xlabel('Positive and Neg',color='darkblue')
ax2.set_xlabel('Positive Only',color='orange')
align_xaxis(ax,0,ax2,0)
ax.invert_yaxis()
plt.show()
This will give you what you're looking for
In my plot, a secondary x axis is used to display the value of another variable for some data. Now, the original axis is log scaled. Unfortunaltely, the twinned axis puts the ticks (and the labels) referring to the linear scale of the original axis and not as intended to the log scale. How can this be overcome?
Here the code example that should put the ticks of the twinned axis in the same (absolute axes) position as the ones for the original axis:
def conv(x):
"""some conversion function"""
# ...
return x2
ax = plt.subplot(1,1,1)
ax.set_xscale('log')
# get the location of the ticks of ax
axlocs,axlabels = plt.xticks()
# twin axis and set limits as in ax
ax2 = ax.twiny()
ax2.set_xlim(ax.get_xlim())
#Set the ticks, should be set referring to the log scale of ax, but are set referring to the linear scale
ax2.set_xticks(axlocs)
# put the converted labels
ax2.set_xticklabels(map(conv,axlocs))
An alternative way would be (the ticks are then not set in the same position, but that doesn't matter):
from matplotlib.ticker import FuncFormatter
ax = plt.subplot(1,1,1)
ax.set_xscale('log')
ax2 = ax.twiny()
ax2.set_xlim(ax.get_xlim())
ax2.xaxis.set_major_formatter(FuncFormatter(lambda x,pos:conv(x)))
Both approaches work well as long as no log scale is used.
Perhaps there exists an easy fix. Is there something I missed in the documentation?
As a workaround, I tried to obtain the ax.transAxes coordinates of the ticks of ax and put the ticks at the very same position in ax2. But there does not exist something like
ax2.set_xticks(axlocs,transform=ax2.transAxes)
TypeError: set_xticks() got an unexpected keyword argument 'transform'
This has been asked a while ago, but I stumbled over it with the same question.
I eventually managed to solve the problem by introducing a logscaled (semilogx) transparent (alpha=0) dummy plot.
Example:
import numpy as np
import matplotlib.pyplot as plt
def conversion_func(x): # some arbitrary transformation function
return 2 * x**0.5 # from x to z
x = np.logspace(0, 5, 100)
y = np.sin(np.log(x))
fig = plt.figure()
ax = plt.gca()
ax.semilogx(x, y, 'k')
ax.set_xlim(x[0], x[-1]) # this is important in order that limits of both axes match
ax.set_ylabel("$y$")
ax.set_xlabel("$x$", color='C0')
ax.tick_params(axis='x', which='both', colors='C0')
ax.axvline(100, c='C0', lw=3)
ticks_x = np.logspace(0, 5, 5 + 1) # must span limits of first axis with clever spacing
ticks_z = conversion_func(ticks_x)
ax2 = ax.twiny() # get the twin axis
ax2.semilogx(ticks_z, np.ones_like(ticks_z), alpha=0) # transparent dummy plot
ax2.set_xlim(ticks_z[0], ticks_z[-1])
ax2.set_xlabel("$z \equiv f(x)$", color='C1')
ax2.xaxis.label.set_color('C1')
ax2.tick_params(axis='x', which='both', colors='C1')
ax2.axvline(20, ls='--', c='C1', lw=3) # z=20 indeed matches x=100 as desired
fig.show()
In the above example the vertical lines demonstrate that first and second axis are indeed shifted to one another as wanted. x = 100 gets shifted to z = 2*x**0.5 = 20. The colours are just to clarify which vertical line goes with which axis.
Don't need to cover them, just Eliminate the ticks!
d= [7,9,14,17,35,70];
j= [100,80,50,40,20,10];
plt.figure()
plt.xscale('log')
plt.plot(freq, freq*spec) #plot some spectrum
ax1 = plt.gca() #define my first axis
ax1.yaxis.set_ticks_position('both')
ax1.tick_params(axis='y',which='both',direction='in');
ax1.tick_params(axis='x',which='both',direction='in');
ax2 = ax1.twiny() #generates second axis (top)
ax2.set_xlim(ax1.get_xlim()); #same limits
plt.xscale('log') #make it log
ax2.set_xticks(freq[d]); #my own 'major' ticks OVERLAPS!!!
ax2.set_xticklabels(j); #change labels
ax2.tick_params(axis='x',which='major',direction='in');
ax2.tick_params(axis='x',which='minor',top=False); #REMOVE 'MINOR' TICKS
ax2.grid()
I think you can fix your issue by calling ax2.set_xscale('log').
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.semilogx(np.logspace(1.0, 5.0, 20), np.random.random([20]))
new_tick_locations = np.array([10., 100., 1000., 1.0e4])
def tick_function(X):
V = X / 1000.
return ["%.3f" % z for z in V]
ax2 = ax.twiny()
ax2.set_xscale('log')
ax2.set_xlim(ax.get_xlim())
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: $X/1000$")