Моя проблема:Как построить график линии с гистограммой в MatPlotLib?
Я havinging горю пытается получить мой график линии для центрируется с у клещей. В настоящее время, похоже, смещение влево, когда я рисую свои графики. Также у меня возникают трудности с попыткой получить оба этикета в одной легенде.
Мой код:
f, ax1 = plt.subplots(1, figsize=(10, 5))
# Set the bar width
bar_width_tram = 1
bar_width_brake_percentage = 1
# Positions of the left bar-boundaries
bar_l = [i + 1 for i in range(len(data_frame['Tram Hrs']))]
# Positions of the x-axis ticks (center of the bars as bar labels)
tick_pos = [i + (bar_width_tram/2) for i in bar_l]
# Define the colours for the corresponding
# bars in the graph
tram_data_hrs_colour = '#00AC00' # Green
bd_per_colour = '#DA0505' # Red
# Create a bar plot, in position bar_1
ax1.bar(bar_l,
# using the pre_score data
data_frame['Tram Hrs'],
# get rid of border lines
edgecolor="none",
# set the width
width=bar_width_tram,
# with the label pre score
label='Tramming Hours',
# with alpha 0.5
alpha=0.05,
# with color
color=tram_data_hrs_colour)
# Set the X-ticks with dates
plt.xticks(tick_pos, data_frame['Date'])
plt.yticks(np.arange(0, 15, 3))
# Set the labels and legend
ax1.set_ylabel("Tramming Time (Hours)")
ax1.set_xlabel("Previous Week")
# Second axis
ax2 = ax1.twinx()
ax2.set_ylabel("Braking to Tramming (%)")
plt.plot(bar_l,data_frame['Brake%'], '-', label='Relative Braking Percentage')
plt.yticks(np.arange(0, 250, 50))
# Set the title of the chart
plt.legend(loc='upper left')
Мои вопросы:
Q1. Как получить линейный график, который будет центрирован/выровнен с x-тиками в гистограммах вдоль оси x (т. Е. В настоящее время они смещены слева)?
Q2. Как я могу получить оба этикета в легенде?
данных:
Date Brake Hrs Tram Hrs Brake% Tram% Br/Tr%
0 Mon 0.87 4.26 16.90 83.10 20.33
1 Tue 1.00 3.05 24.66 75.34 32.73
2 Wed 1.77 3.87 31.44 68.56 45.85
3 Thu 1.86 5.16 26.44 73.56 35.94
4 Fri 1.41 2.01 41.15 58.85 69.93
5 Sat 0.01 5.03 0.14 99.86 0.14
6 Sun 0.40 1.16 25.82 74.18 34.82
Использование столбцов Date
для оси х, а Tram Hrs
ось 1, Br/Tr%
для оси 2
Не могли бы вы дать мне полный код – SSN
@SatheeshSivaNallathambi Данные извлекаются из базы данных - я могу поделиться с вами набором данных, если хотите? – 3kstc