如何在Python中旋转以下图并更改X和Y数据

yk9xbfzb  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(82)

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)

字符串

gwbalxhn

gwbalxhn1#

您需要使用kind = 'barh',但也需要更改label_group_bar_table,以便标签沿着y轴放置。基本上交换x和y,但考虑到可能的重叠将发生在不同的级别之间,而不再是在同一级别上。
为了避免这种情况,我旋转级别0并将长度超过15个字符的标签换行。请随意修改代码(还要注意,定义df可以在单个表达式中完成):

import pandas as pd
import matplotlib.pyplot as plt
from itertools import groupby
import numpy as np
from textwrap import wrap

label_max_len = 15

# 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(values, index=index, columns=['In_situ', 'Remote_sensing'])

def add_line(ax, xpos, ypos):
    line = plt.Line2D([xpos + .1, xpos], [ypos, 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):
    xpos = -0.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):
            lypos = (pos + .5 * rpos)*scale
            orient = 'vertical' if level==0 else 'horizontal'
            ax.text(xpos, lypos, '\n'.join(wrap(label, label_max_len)), va='center', ha='center', transform=ax.transAxes, rotation=orient)
            add_line(ax, xpos, pos*scale)
            pos += rpos
        add_line(ax, xpos, pos*scale)
        xpos -= 0.1

ax = df.plot(kind='barh', stacked=False)
ax.set_yticklabels('')
ax.set_ylabel('')
label_group_bar_table(ax, df)

plt.subplots_adjust(left=0.2)
plt.show()

字符串
输出量:


的数据

相关问题