gpt4 book ai didi

python - matplotlib 图中的条形大小不一致

转载 作者:行者123 更新时间:2023-11-28 19:03:28 25 4
gpt4 key购买 nike

***忽略这​​个问题:问题是我没有在列表中给出零和保留日期来生成图表。如果没有包含零的日期,matplotlib 会正确地尝试跨越以填充空间。我需要做的是构建一个函数来填充缺失的日期或在原始调用中提供缺失的日期。

好图:

good graph
(来源:warfightercoffeeshop.com)

坏图:

good graph

我有一个很难解决的问题。我正在尝试创建许多一致的图形并将一个 matplotlib 类放在一起来处理该问题。一切正常,我通常有一致的输出。但是如果 x 轴在所有日期都没有数据点,我会得到非常宽的条形图。我会尝试发布图片,但我的声誉是个问题。

有谁知道怎么调用看看坐标轴大小有没有变化?这都是相对于 x 大小的,所以如果我能相对于 x 条目的数量算出它可能有效吗?任何帮助将不胜感激。

调用代码如下:

test_chart = HPM_Chart_Gen_class.HPM_Graph(x, title, "Date", "Hours", is_date=True, span_dates = False)
test_chart.add_y_axis(y2, "Spindle Hours", type="bar", trendline=False, datalables=True)
test_chart.add_y_axis(y, "Billable Hours", type="bar", trendline=False, datalables=True)

test_chart.print_chart(r'M:\ZZ_Continuous Improvement\Dashboard_Pictures\\' + current + r'.png')

类:

class new_y_axis:

def __init__(self, data, label, type, trendline, datalables):
#type can be bar
self.type = type
self.data = data
self.label = label
self.trendline = trendline
self.datalables = datalables


class HPM_Graph:

def __init__(self, x_axis, title, x_axis_label, y_axis_label, width=15, height=10, fontsize=12, is_date = False, span_dates = False):
self.width = width
self.height = height
self.fontsize = fontsize
self.x_axis_label = x_axis_label
self.y_axis_label = y_axis_label
self.x_axis = x_axis
self.is_date = False
self.y_axis = []
self.multiple_bar = False
#this colors list is the maximum y values available, you should never go over this
self.colors_list = ['#5DA5DA', '#FAA43A', '#60BD68', '#F15854', '#B2912F', '#DECF3F','#B276B2', '#29aeec', '#e3b02e', '#024d6a', '#b45757', '#fb0933', '#96c71c', '#b9aaee', '#801a42', '#e56297', '#01e0f4', '#d0aa1c']
self.is_date = is_date
self.title = title
self.span_dates = span_dates
self.x_axis_is_string = False
self.x_axis_is_string_labels = []
self.test_x_axis_for_strings()
self.number_of_bars = 0

def add_y_axis(self, data, label,type="plot",trendline=False, datalables=True):
self.y_axis.append(new_y_axis(data,label,type,trendline,datalables))
if type == "bar":
self.number_of_bars=self.number_of_bars+1

def print_chart(self, save_path):
self.width_interval_to_use = 1
matplotlib.rcParams.update({'font.size': self.fontsize})
self.figure = plt.figure(figsize=(self.width,self.height))
self.ax = self.figure.add_subplot(111)
axis_count_bar = 0
if self.is_date == True:
#we need to see if this is a date range and split accordingly - has to be done before dates are turned into float
self.x_axis = date2num(self.x_axis)
self.formatter = DateFormatter('%m-%d-%y')
# self.ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
days = mdates.DayLocator() # every month
self.ax.xaxis.set_minor_locator(days)
self.ax.xaxis.set_major_formatter(self.formatter)
if self.x_axis_is_string == True:
print(self.x_axis_is_string_labels)
self.ax.set_xticklabels(self.x_axis_is_string_labels)
self.ax.tick_params(width=2)
self.ax.set_xticks(self.x_axis, minor=True)
self.axis_ittirator()
self.ax.legend(loc=0)
self.ax.set_title(self.title)
self.ax.grid(True)
self.ax.set_xlabel(self.x_axis_label)
self.ax.set_ylabel(self.y_axis_label)
self.figure.autofmt_xdate()
self.figure.savefig(save_path)
print(save_path)

def autolabel(self, rects):
for rect in rects:
height = rect.get_height()
self.ax.text(rect.get_x() + rect.get_width()/2., 1.02*height,
'%d' % int(height),
ha='center', va='bottom')

def axis_ittirator(self):
self.multiple_bar = True
#margin gen sets the bar width as 80% at all times
margin_gen = 0.8
self.bar_width = margin_gen/self.number_of_bars
self.bar_index = -(margin_gen/2)
for current_y_axis in self.y_axis:
color = self.colors_list[0]
self.colors_list.remove(color)
if current_y_axis.trendline == True:
z = np.polyfit(self.x_axis, current_y_axis.data, 1)
py1 = np.poly1d(z)
label_string = f"{current_y_axis.label} trend"
color_string = f"--"
self.ax.plot(self.x_axis,py1(self.x_axis), "--", color=color, label=label_string)
if current_y_axis.type == "bar":
# add = self.bar_index-0.5
add = self.bar_index
temp_x_axis = [x+add for x in self.x_axis]
print(f"adding {add} for index {self.bar_index}")
resection = self.ax.bar(temp_x_axis,current_y_axis.data, align="edge", color=f"{color}", width=self.bar_width, label=current_y_axis.label)
self.bar_index = self.bar_index + self.bar_width
else:
self.ax.plot(self.x_axis,current_y_axis.data, color=f"{color}", label=current_y_axis.label)
if current_y_axis.datalables == True:
self.autolabel(resection)

def test_x_axis_for_strings(self):
for current_x_axis_value in self.x_axis:
if isinstance(current_x_axis_value, str):
self.x_axis_is_string = True
if self.x_axis_is_string:
self.is_date = False
Axis_holder = copy.deepcopy(self.x_axis)
self.x_axis = []
self.i = 0
for current_heald_x_axis_value in Axis_holder:
self.i = self.i+1
self.x_axis.append(self.i)
self.x_axis_is_string_labels.append(current_heald_x_axis_value)

最佳答案

忽略这个问题:问题是我没有在列表中给出零和保留日期来生成图表。如果没有包含零的日期,matplotlib 会正确地尝试跨越以填充空间。我需要做的是构建一个函数来填充缺失的日期或在原始调用中提供缺失的日期。

关于python - matplotlib 图中的条形大小不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49945750/

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