DNB5brims
DNB5brims

Reputation: 30578

How to manipulate data in Python 3 with matplotlib?

I have the following data:

[0.21, 0.21, 0.33, 0.52, 0.22, 0.35, 0.43]

I would like to do the following things:

  1. Draw a bar chart like this: enter image description here The x-axis should be 0.21, 0.22, 0.33, 0.35, 0.43, 0.52.

  2. The second things I would like to do is using a value range, for example, I would like to change the x-axis to : 0.01- 0.2, 0.2-0.4, 0.4-0.6 Instead of loop it one by one, is there any smarter way?

Upvotes: 1

Views: 45

Answers (1)

MartyFizzle
MartyFizzle

Reputation: 25

Part 2. of your question is simple enough to do - you just need to define the width of the bins by passing a list of the boundaries.

import matplotlib.pyplot as plt

X = [0.21, 0.21, 0.33, 0.52, 0.22, 0.35, 0.43]
plt.hist(X, bins=[0.0, 0.2, 0.4, 0.6])
plt.show()

This will create bins [0.0, 0.2), [0.2, 0.4), [0.4, 0.6] where '[' is inclusive and '(' is exclusive.

Not clear on what you require in part 1. of your question.

Upvotes: 1

Related Questions