mubas007
mubas007

Reputation: 131

How to plot two list of tuples in Pandas using matplotlib?

Hello I have a dataframe here called temp, when I do temp["diameter_tuple"].unique() I get the following table.
This table actually represents the speed ranges of a motor on the left and the diameters of the rod produced using these speed ranges on the right. I want to plot a graph that plots the speed ranges (left) on the X-axis and the corresponding diameters of the rod produced for this speed range on the Y-axis. Note: There are also some speed ranges for which there are more than one diameter range produced for e.g. here:

(45, 45.5) [(-2.65, 6.35), (0.625, 0.75)]

that is for the speed range of (45, 45.5) the diameter range of (-2.65, 6.35) and (0.625, 0.75) was produced.

speed_range_tuple   

    (20.5, 21.0)                                   [(0.55, 0.75)]
    (23.5, 24.0)                                  [(0.65, 0.715)]
    (24, 24.5)                                    [(0.645, 0.75)]
    (25, 25.5)                                     [(0.795, 0.9)]
    (25.5, 26.0)                                     [(0.5, 0.8)]
    (26.5, 27.0)                                  [(0.745, 0.85)]
    (28, 28.5)                                    [(0.645, 0.75)]
    (30, 30.5)                                    [(-2.75, 6.25)]
    (30.5, 31.0)                                    [(0.7, 0.78)]
    (31.5, 32.0)                                   [(0.65, 0.73)]
    (35, 35.5)                                    [(-0.25, 8.75)]
    (35.5, 36.0)                         [(0.5, 0.8), (0.2, 9.2)]
    (45, 45.5)                     [(-2.65, 6.35), (0.625, 0.75)]
    (47, 47.5)                                    [(-1.65, 7.35)]
    (47.5, 48.0)                                  [(0.65, 0.775)]
    (50, 50.5)      [(-7.15, 1.85), (0.725, 0.85), (0.625, 0.75)]
    Name: diameter_tuple, dtype: object

So I want to plot the above table to something like this (I hope this helps in understanding)

enter image description here

Upvotes: 0

Views: 227

Answers (2)

filiphl
filiphl

Reputation: 991

A two-dimensional scatter plot would not be able to describe the ranges intuitively. I would suggest plotting a line from (min_speed, min_dim) to (max_speed, max_dim). Something like the following might do it. The bottom subplot is simply a close-up of the top one.

from matplotlib import pyplot as plt

s = temp["diameter_tuple"].unique()

fig, ax = plt.subplots(nrows=2, sharex=True, figsize=(10,10))
for i in range(len(s)):
    for j in range(len(s[i])):
        ax[0].plot(s.index[i], s[i][j])
        ax[1].plot(s.index[i], s[i][j])
ax[1].set_xlabel("Speed")
ax[0].set_ylabel("Diameter")
ax[1].set_ylabel("Diameter")
ax[1].set_ylim([0.2,1])

enter image description here

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 71689

One way to plot the given data:

df = temp["diameter_tuple"].unique().reset_index()
df = df.explode('diameter_tuple').sort_values(by='speed_range_tuple')
df[['speed_range_tuple', 'diameter_tuple']].astype(str).plot.scatter(
    'speed_range_tuple', 'diameter_tuple', s=50)
plt.show()

Sample output:

enter image description here

Upvotes: 1

Related Questions