Issue
I want to create a matplotlib event plot of integers with two colours. A position in the eventplot can be white (no event) or have a colour (red or green, which means there is an event).
For this I have the following code:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.rcParams['font.size'] = 8.0
# create data
data1 = np.array([[19, 2, 3, 4, 6, 34 , 23, 60, 49, 36], [ 52, 50, 33, 96, 56, 95, 0, 63, 90, 15]]) # alternative 1
#data1 = np.array([[19, 2, 3, 4, 6, 34 , 3, 200, 49, 36], [ 52, 50, 33, 96, 56, 95, 0, 63, 90, 15]]) # alternative 2
colors1 = ["green", "orange"]
lineoffsets1 = [1, 1]
linelengths1 = [1, 1]
linewidths = np.full(2, 3) #alternative 1
#linewidths = np.full(2, 4) #alternative 2
print(linewidths)
fig, axs = plt.subplots()
# create a horizontal plot
axs.eventplot(data1, colors=colors1, lineoffsets=lineoffsets1, linelengths=linelengths1, linewidths=linewidths)
plt.show()
At the same time, I want to avoid white spaces between events in case that there are events in adjacent integer positions (events at x values 2, 3, and 4 should not show any spacing between the numbers, but between events at 4 and 6 there should be a white spacing of the same width as the events.
The integer range of the events may vary.
I can control the width of the events using the parameter linewidths, but I am unaware where to get an overall width from.
EDIT: The effect seems to also depend on the actual scaling of the window, so if I increase the size of the window with the mouse, lines do not touch any longer. Maybe one needs to take a completely different approach
Solution
I think using a bar plot is a better choice for your case:
fig, axs = plt.subplots()
for i, data in enumerate(data1):
for event in data:
axs.barh(0, width=1, left=event, height=1, color='green' if i == 0 else 'orange')
plt.show()
You can also use fill_between
, but using a bar plot is simpler and more reasonable for your case.
Answered By - fatemeh_k95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.