I would to rotate the image where value will be place in X axis and other data in Y axis
I am expecting something like that的
我尝试了以下代码。我在寻求帮助。非常感谢您的支持。
import padas as pd
import matplotlib.pyplot as plt
from itertools import groupby
import numpy as np
%matplotlib inline
# Create sample DataFrame with MultiIndex
Satellite = ['Image fusion', 'Landsat 8 OLI', 'Sentinel 2A MSI']
Reference = ['Batur and Maktav, 2019', 'Cao et al., 2022', 'Krishnaraj and Honnasiddaiah, 2022', 'Pereira et al., 2020','Qian et al., 2022'],
Type = ('In_situ', 'Remote_sensing')
index = pd.MultiIndex.from_tuples([('Image fusion','Batur and Maktav, 2019'),('Landsat 8 OLI','Cao et al., 2022'),('Landsat 8 OLI','Krishnaraj and Honnasiddaiah, 2022'),('Landsat 8 OLI','Pereira et al., 2020'), ('Sentinel 2A MSI','Qian et al., 2022')],
names=['Satellite', 'Reference'])
values = np.array([[8.29, 8],[7.56, 7.5],[7.8, 7.9], [7.85, 8.165],[8.31, 8.9]])
df = pd.DataFrame(index=index)
df['In_situ'] = values[:,0]
df['Remote_sensing'] = values[:,1]
def add_line(ax, xpos, ypos):
line = plt.Line2D([xpos, xpos], [ypos + .1, ypos],
transform=ax.transAxes, color='gray')
line.set_clip_on(False)
ax.add_line(line)
def label_len(my_index,level):
labels = my_index.get_level_values(level)
return [(k, sum(1 for i in g)) for k,g in groupby(labels)]
def label_group_bar_table(ax, df):
ypos = -.1
scale = 1./df.index.size
for level in range(df.index.nlevels)[::-1]:
pos = 0
for label, rpos in label_len(df.index,level):
lxpos = (pos + .5 * rpos)*scale
ax.text(lxpos, ypos, label, ha='center', transform=ax.transAxes)
add_line(ax, pos*scale, ypos)
pos += rpos
add_line(ax, pos*scale , ypos)
ypos -= .1
ax = df.plot(kind='bar',stacked=False)
ax.set_xticklabels('')
ax.set_xlabel('')
label_group_bar_table(ax, df)
字符串
1条答案
按热度按时间gwbalxhn1#
您需要使用
kind = 'barh'
,但也需要更改label_group_bar_table
,以便标签沿着y轴放置。基本上交换x和y,但考虑到可能的重叠将发生在不同的级别之间,而不再是在同一级别上。为了避免这种情况,我旋转级别0并将长度超过15个字符的标签换行。请随意修改代码(还要注意,定义df可以在单个表达式中完成):
字符串
输出量:
的数据