简体   繁体   中英

How to show truncated X-axis in matplotlib (seaboard) heatmap

I have the following image:

在此处输入图片说明

Created with this code:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# create some random data
data = pd.DataFrame(np.random.rand(11, 5), columns=['A', 'B', 'C', 'D', 'E'], index = ['yyyyyyyy - ' + str(x) for x in range(2000, 2011, 1)])

# plot heatmap
ax = sns.heatmap(data.T)

# turn the axis label
for item in ax.get_yticklabels():
    item.set_rotation(0)

for item in ax.get_xticklabels():
    item.set_rotation(90)

# save figure

plt.savefig('seabornPandas.png', dpi=100)
plt.close()

My question is how can I adjust the image so that the truncated x-axis showed its full string?

There's a convenient way to do this through a subplots_adjust method:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# create some random data
data = pd.DataFrame(np.random.rand(11, 5), columns=['A', 'B', 'C', 'D', 'E'], index = ['yyyyyyyy - ' + str(x) for x in range(2000, 2011, 1)])

# plot heatmap
plt.ion()
ax = sns.heatmap(data.T)
# adjust to taste
ax.figure.subplots_adjust(bottom = 0.25)

# turn the axis label
for item in ax.get_yticklabels():
    item.set_rotation(0)

for item in ax.get_xticklabels():
    item.set_rotation(90)

# save figure
plt.savefig('seabornPandas.png', dpi=100)

X轴空间

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM