matplotlib 在断开的条形图上分配彩色类别时遇到的问题

4nkexdtk  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(99)

为什么我得到这样延长的最终颜色(绿色)部分到这些条?
每个GUI框都应该采用两个逗号分隔的整数作为该类别的开始和持续时间。每个类别的数字应该稍微大一点,这样颜色就不会重叠。这可以在GUI的所有行中输入,每个框中有一对数字:1,910,1920,2930,3940,49任何帮助将不胜感激。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import tkinter as tk

# Define the data as a dictionary
data = {
    'x_values': [],
    'y_values': [(6.0, 2), (8.5, 2), (12.5, 2), (15.0, 2), (19.0, 2)]
}

# Define a list of colors and categories for the bars
colors = ('tab:red', 'tab:orange', 'tab:purple', 'tab:blue', 'tab:green')
categories = ('Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5')
jobs = ('Job1', 'Job2', 'Job3', 'Job4', 'Job5')

# Create a GUI interface to get user input for x_values
def get_x_values():
    global data
    x_values = []
    for i in range(len(jobs)):
        values_list = []
        for j in range(len(categories)): 
            x, y = x_values_entry[i][j].get().split(',')
            values_list.append((int(x), int(y)))
        x_values.append(values_list)
    data['x_values'] = x_values
    root.destroy()

root = tk.Tk()

# Create a grid of input boxes for the x_values
x_values_entry = []
for i in range(len(jobs)):
    row = []
    for j in range(len(categories)):
        if i == 0:
            label = tk.Label(root, text=categories[j])
            label.grid(row=i, column=j+1)
        if j == 0:
            label = tk.Label(root, text=jobs[i])
            label.grid(row=i+1, column=j)
        entry = tk.Entry(root, width=10)
        entry.grid(row=i+1, column=j+1)
        row.append(entry)
    x_values_entry.append(row)

# Add a button to submit the x_values and close the GUI
submit_button = tk.Button(root, text='Submit', command=get_x_values)
submit_button.grid(row=len(jobs)+1, columnspan=len(categories)+1)

# Run the GUI
root.mainloop()

# Define a list of colors and categories for the bars
colors = ('tab:red', 'tab:orange', 'tab:purple', 'tab:blue', 'tab:green')
categories = ('Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5')

# Add the colors and categories to each row of the DataFrame
for i in range(len(data['x_values'])):
    data['facecolors'] = [colors] * len(data['x_values'])
    data['categories'] = [categories] * len(data['x_values'])

# Create a pandas DataFrame from the data
df = pd.DataFrame(data)

# Create a new figure and axis
fig, ax = plt.subplots(figsize=(10,6))

# Loop through each row of the DataFrame and plot the broken bar chart
for i, row in df.iterrows():                             #makes no dif 0 or i here^
    ax.broken_barh(row['x_values'], row['y_values'], facecolors=row['facecolors'])
    #ax.broken_barh(row['x_values'], row['y_values'], facecolors=row['facecolors'])

# Create legend entries with color rectangles and category labels
legend_entries = [Patch(facecolor=color, edgecolor='black', label=category) for color, category in zip(colors, categories)]

# Add the legend to the plot
ax.legend(handles=legend_entries, loc='upper right', ncol=5, bbox_to_anchor=(1.0, 1.00))

# Customize the axis labels and limits
ax.set_xlabel('Days')
ax.set_ylabel('Jobs')
ax.set_yticks([7, 9.5, 13.5, 16, 20], labels=['Job1', 'Job2', 'Job3','Job4', 'Job5'])
                                                    
                                                  
title = ax.set_title('Tasks and Crew-Days')
title.set_position([0.5, 1.0])               #set title at center
ax.set_ylim(5, 26)
ax.grid(True)

# Display the plot
plt.show()
bgtovc5b

bgtovc5b1#

'def get_x_values'需要替换为以下代码,以消除从GUI输入值中读取任何空白。代码对我来说很好,对split()行做了这个轻微的更改:

# Create a GUI interface to get user input for x_values
def get_x_values():
    global data
    x_values = []
    for i in range(len(jobs)):
        values_list = []
        for j in range(len(categories)): 
            x, y = x_values_entry[i][j].get().replace(' ', '').split(',')
            values_list.append((int(x),int(y)))
        x_values.append(values_list)
    data['x_values'] = x_values
    print(x_values)
    root.destroy()

root = tk.Tk()

相关问题