NicoCaldo
NicoCaldo

Reputation: 1577

Show all points in a plot with Pyplot on Python

So I'm new to Pyplot and I was able to show a standard x,y plot.

Now my issue is that I have a series of point in the form of (time,percentage) but I have multiple percentage with the same time. For istance:

x(time) y(percentage)
0:0:1   3
0:0:1   4
0:0:1   2
0:0:1   1
0:0:2   1
0:0:2   3
0:0:2   2

For some reason plt.plot plot only one value for 0:0:1 and 0:0:2 but I would like to plot every value on the x axis, even if they have the same value.

I'm using plt.gcf().autofmt_xdate() for the x axis

Is there a way to do that?

Upvotes: 0

Views: 973

Answers (1)

ripalo
ripalo

Reputation: 108

Not sure if this helps as I'm not sure if your dataset is fixed, but you could display it thru plt.xticks though you might need to change or modify your initial dataset.

Assuming below is your code:

data = {
    'x(time)':['0:0:1a','0:0:1b','0:0:1c','0:0:1d','0:0:2e','0:0:2f','0:0:2g'],
    'y(percentage)': [3,4,2,1,1,3,2]
}
print(data)
df = pd.DataFrame(data=data)
print(df)
your_label = ['0:0:1','0:0:1','0:0:c','0:0:1','0:0:2','0:0:2','0:0:2']

Then you would plot it using the code:

plt.scatter(x='x(time)', y='y(percentage)', data=df)
plt.xticks(ticks=df['x(time)'], labels=your_label)
plt.show()

plt.xticks will generate new labels for your plot. Of course, you could use a generator like np.arange to generate the labels for you, but that would be dependent on whether your initial dataset can be modified in the first place.

I look forward to other people's reply too.

Upvotes: 1

Related Questions