gpt4 book ai didi

python - 如何使用 Python 显示值构建从正百分比到负百分比的水平堆积条形图

转载 作者:行者123 更新时间:2023-12-02 20:03:49 26 4
gpt4 key购买 nike

我有一个数据框,但我正在尝试构建一个图表,但遇到了困难。你们能帮我解决这个问题吗?我试着用谷歌搜索,但找不到有效的资源。

MY DATA FRAME

I am trying to build a graph similar to the below graph

最佳答案

friend ,你的图表引起了我的注意,我决定创建它。在这个过程中我学到了很多东西,希望对你有所帮助:

Favorability plot

输入数据:

    Completely Dissatisfied Somewhat Dissatisfied   Dissatisfied    Neither Satisfied nor Dissatisfied  Somewhat Satisfied  Satisfied   Completely Satisfied
Q1 10 10 10 10 10 20 30
Q2 0 0 20 20 30 20 10
Q3 10 20 0 30 20 20 20
Q4 0 0 10 10 30 20 10

绘图代码:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import pandas as pd

# Read in data
df = pd.read_csv('plot_data.csv', index_col=0)
df = df/100 # Convert to percentages

def custom_plot(df, example=False):

def plot_rect(bottom, left, width, height, color = 'C0'):
ax.add_patch(patches.Rectangle(
(left, bottom), width, height, linewidth=1, edgecolor=color, facecolor=color))

# Create figure and axes
fig, ax = plt.subplots(1)

# Define axis ticks ticks
plt.xticks(np.arange(-1,1.25,0.25), np.arange(-100,125,25))
plt.yticks(np.arange(0,1.2,0.2), np.arange(0,1.2,0.2))

# Define axis limits
plt.ylim(0.05,0.95)
plt.xlim(-1.125, 1.125)

# Move gridlines to the back of the plot
plt.gca().set_axisbelow(True)

# Change color of plot edges
ax.spines['left'].set_color('lightgray')
ax.spines['right'].set_color('lightgray')
ax.spines['top'].set_color('lightgray')

# Hide y axis ticks
plt.gca().tick_params(axis='y', colors='w')

# Turn on gridlines and set color
plt.grid(b=True, axis='both', color='lightgray', alpha=0.5, linewidth=1.5)

# Add lines
plt.axvline(x=0, c='lightgray')
plt.axhline(y=0.5, c='black')

# Add x label
plt.xlabel('Percent', fontsize=14)

# Define color scheme from negative to positive
colors = ['firebrick', 'sandybrown', 'navajowhite',
'khaki', 'lightcyan', 'skyblue', 'steelblue']

# Process data to plot
try:
array = [df.iloc[0,:].values,
df.iloc[1,:].values,
df.iloc[2,:].values,
df.iloc[3,:].values]
except:
print('Plotting example data')
example = True

if example == True:
# Example data
array = [np.array([0.05, 0.1, 0.2, 0.2, 0.3, 0.1, 0.05]),
np.array([0, 0.1, 0.1, 0.3, 0.2, 0.2, 0.1]),
np.array([0.1, 0.2, 0.2, 0.3, 0.1, 0.05, 0.05]),
np.array([0.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1])]

# Example data column names
df = pd.DataFrame(columns=['Completely Dissatisfied',
'Somewhat Dissatisfied',
'Dissatisfied',
'Neither Satisfied nor Dissatisfied',
'Somewhat Satisfied',
'Satisfied',
'Completely Satisfied'])
# Compute average statistics
hi = [sum(x[4:]) for x in array]
med = [x[3] for x in array]
lo = [sum(x[0:3]) for x in array]

# Define function to process input data into rectangle left corner indices
def process_data(array):
left = np.zeros_like(array)
mid = array[3]/2
left[0] = -np.sum(array[0:3]) - mid
left[1] = -np.sum(array[1:3]) - mid
left[2] = -np.sum(array[2:3]) - mid
left[3] = -mid
left[4] = mid
left[5] = np.sum(array[4:5]) + mid
left[6] = np.sum(array[4:6]) + mid
width = array
return left, width

left = {}
width = {}
for i in range(4):
left[i], width[i] = process_data(array[i])

# Plot boxes
height = 0.13
bottom = 0.135
for i in range(len(array)):
for j in range(len(array[i])):
plot_rect(bottom=bottom+i*0.2, left=left[i][j], width=width[i][j], height=height, color = colors[j])

# Plot category labels
plt.text(-1.1,0.9,'Unfavorable', style='italic',
horizontalalignment='left', verticalalignment='center')
plt.text(0,0.9,'Neutral', style='italic',
horizontalalignment='center', verticalalignment='center')
plt.text(1.1,0.9,'Favorable', style='italic',
horizontalalignment='right', verticalalignment='center')

# Plot percentages
for i in range(len(med)):
plt.text(-1,0.2*(i+1),'{0:.0%}'.format(lo[i]),
horizontalalignment='left', verticalalignment='center')
plt.text(0,0.2*(i+1),'{0:.0%}'.format(med[i]),
horizontalalignment='center', verticalalignment='center')
plt.text(1,0.2*(i+1),'{0:.0%}'.format(hi[i]),
horizontalalignment='right', verticalalignment='center')

# Create legend
fig, ax = plt.subplots(1, figsize=(6,2))
plt.axis('off')
plt.gca().set_aspect('equal', adjustable='box')

# Plot colored circles
legend_left = [-0.9, -0.6, -0.3, 0, 0.30, 0.6, 0.9]
for i in range(len(colors)):
plot_rect(bottom=0, left=legend_left[i], width=0.2, height=0.2, color = colors[i])

# Plot labels 1-6
for i in range(0,6,2):
plt.text(-0.8+0.3*i, 0.25, df.columns[i].replace(' ', '\n'),
horizontalalignment='center', verticalalignment='bottom')
plt.text(-0.5+0.3*i, -0.05, df.columns[i+1].replace(' ', '\n'),
horizontalalignment='center', verticalalignment='top')

# Plot last label
plt.text(1, 0.25, df.columns[6].replace(' ', '\n'),
horizontalalignment='center', verticalalignment='bottom')

# Plot label title
plt.text(-1, 0.1, 'Scale', fontsize=14,
horizontalalignment='right', verticalalignment='center')

plt.gca().autoscale(enable=True, axis='both', tight=None)

#custom_plot('example')
custom_plot(df)

关于python - 如何使用 Python 显示值构建从正百分比到负百分比的水平堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com