I have generated the plot below for a friend. How can I expand the plot, so the tick labels are not cut off? And how do I left align the y-axis tick labels and align them tightly to the axis?
Here is the input file una.txt:
#Average (pg/mL) No Il-2 TGFbeta1 IL-2 TGFbeta1 IL-2 TGFbeta1 50 ngmL TNF-alpha IL-2 TGFbeta1 500 ngmL TNF-alpha IL-2 TGFbeta1 10 ngmL IL-1beta IL-2 TGFbeta1 100 ngmL IL-1beta 50 ngmL TNF-alpha 10 ngmL IL-1beta 500 ngmL TNF-alpha 100 ngmL IL-1beta
IL-9 29.53333333 13568.975 12761.7675 9446.42 9973.11 9844.95 8227.38 8955.19
IL-10 13.415 18.04833333 30.22875 32.815 26.43875 31.945 32.73125 36.87625
IFN-gamma 3380.6225 11405.42 11129.15 11359.28667 11263.78667 11557.82 9413.87 11814.92
IL-17 1.705 6.2025 7.11625 5.485 5.98625 6.79125 7.57625 9.73
TNF-alpha 1780.542857 2082.36 6405.03875 5134.015 2209.301667 1938.536667 5335.241429 9923.431429
IL-1beta 21.144 21.096 48.33333333 46.775 2182.01 20023.676 2614.72 19148.32
IL-2 42.56166667 10263.83667 11560.08667 11963.778 14336.2925 13951.88 13017.13 14915.488
IL-6 100.2433333 196.9666667 233.1133333 210.3383333 542.265 594.2375 730.1183333 560.94
IL-21 8.513333333 112.3333333 104.9228571 90.0375 105.6975 142.205 105.61625 138.9716667
IL-22 99.07 86.05 75.57666667 70.53333333 86.45666667 119.2125 93.78666667 95.6075
IL-23 27.72 81.395 72.89 65.305 70.19666667 61.99 95.62 90.1575
IL-4 104.86625 112.05625 119.52875 102.7775 120.60875 115.775 113.12375 112.36375
IL-5 703.17 444.29625 546.255 442.9175 538.66875 569.0585714 532.565 538.6
IL-12p70 6.263333333 6.77 7.4325 9.64 11.695 12.27666667 12.21 12.23857143
IL-18 221.6375 268.73 247.2725 255.8125 232.2266667 270.1475 262.3966667 269.495
IL-13 3365.457143 5300.827143 5108.098333 3604.03 5037.185714 3931.995 6778.05 5125.295
IL-27 49.78470588 70.2025 30.695 34.016 70.115 52.73166667 87.27 33.16
GM-CSF 8542.278333 11626.19667 9663.6175 18898.80143 11864.3125 12143.33857 28017.90857 36413.00833
Here is my code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
colors = [
'#e41a1c', '#377eb8', '#4daf4a', '#984ea3',
'#ff7f00', '#ffff33', '#a65628', '#f781bf',
]
a = np.genfromtxt('una.txt', delimiter='\t', usecols=(1, 2, 3, 4, 5, 6, 7, 8))
fig = plt.figure(tight_layout=True)
ax = fig.add_subplot(111, projection='3d')
xticklabels = np.genfromtxt('una.txt', delimiter='\t', usecols=(0), dtype=None)
with open('una.txt') as f:
yticklabels = [s.replace('alpha', r'$\alpha$').replace('gamma', r'$\gamma$').replace('beta', r'$\beta$') for s in f.readline().rstrip().split('\t')[1:]]
ax.set_xticks(np.arange(0, len(xticklabels), 1))
ax.set_xticklabels(
[label.decode("utf-8").replace('alpha', r'$\alpha$').replace('gamma', r'$\gamma$').replace('beta', r'$\beta$') for label in xticklabels],
rotation='vertical', fontsize=6)
ax.set_yticks(np.arange(0, len(yticklabels), 1))
ax.set_yticklabels(yticklabels, fontsize=6, ha='left')
ax.tick_params(axis='z', labelsize=6)
for c, z in zip(colors, np.arange(0, 7, 1)):
xs = np.arange(a.shape[0])
ys = a[:,z]
ax.bar(xs, ys, zs=z, zdir='y', color=[c] * len(xs), alpha=0.8) # yerr=[]
ax.set_zlabel('Average (pg/mL)')
plt.tight_layout()
#plt.show()
plt.savefig('una.png', bbox_inches='tight', dpi=600)
plt.close()
I don't know another solution than to play around with the view settings azim
, elev
and dist
, for example:
import numpy as np
import matplotlib.pyplot as pl
from mpl_toolkits.mplot3d import Axes3D
x,y = np.meshgrid(np.linspace(0,1000,10),np.linspace(0,1000,15))
z = np.random.random(x.shape)
pl.figure()
ax = pl.subplot(121, projection='3d')
ax.plot_surface(x,y,z)
ax = pl.subplot(122, projection='3d')
ax.plot_surface(x,y,z)
ax.dist = 20
It's a bit of a crude example, and it doesn't make the figure better readable, but it stops the axis labels from being cropped in the right figure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.