0

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

Zephyr
  • 11,891
  • 53
  • 45
  • 80
mubas007
  • 129
  • 5

2 Answers2

1

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

Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
  • Thanks you for the reply, but I get this error TypeError: explode() takes 1 positional argument but 2 were given – mubas007 Jun 18 '20 at 09:35
  • Can you show the output of `temp["diameter_tuple"].unique().reset_index()`? – Shubham Sharma Jun 18 '20 at 09:45
  • 1
    Okay I got the graph it seems the error was from my end thank you. Also how did you control the font sizes on the axis. All of my values on the axis are getting cluttered up with each other. – mubas007 Jun 18 '20 at 09:59
  • Great! use `plt.gca().tick_params(axis='x', labelsize=10)` . For more info refer [to this answer](https://stackoverflow.com/questions/6390393/matplotlib-make-tick-labels-font-size-smaller?noredirect=1&lq=1) – Shubham Sharma Jun 18 '20 at 10:03
1

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

filiphl
  • 921
  • 1
  • 8
  • 15