6

I am trying to plot line graphs in matplotlib with the following data, x,y points belonging to same id is one line, so there are 3 lines in the below df.

      id     x      y
0      1    0.50    0.0
1      1    1.00    0.3
2      1    1.50    0.5
4      1    2.00    0.7
5      2    0.20    0.0
6      2    1.00    0.8
7      2    1.50    1.0
8      2    2.00    1.2
9      2    3.50    2.0
10     3    0.10    0.0
11     3    1.10    0.5
12     3    3.55    2.2

It can be simply plotted with following code:

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib notebook

fig, ax = plt.subplots(figsize=(12,8))
cmap = plt.cm.get_cmap("viridis")
groups = df.groupby("id")
ngroups = len(groups)

for i1, (key, grp) in enumerate(groups):
    grp.plot(linestyle="solid", x = "x", y = "y", ax = ax, label = key)

plt.show()

But, I have another data frame df2 where weight of each id is given, and I am hoping to find a way to control the thickness of each line according to it's weight, the larger the weight, thicker is the line. How can I do this? Also what relation will be followed between the weight and width of the line ?

  id     weight
0  1          5
1  2         15
2  3          2

Please let me know if anything is unclear.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
abcdef
  • 105
  • 1
  • 1
  • 6
  • linewidth="foo" Then explore and see what you want to set foo to. Chances are, you want to scale your weight to be between 1 and 7ish - much beyond that is obscenely huge. How you do that depends entirely on the range of your weight. – Scott Mermelstein Aug 23 '17 at 18:34
  • 1
    Well, I guess the actual problem is unclear. You described the task, but not the problem. Have you tried anything yet? What was the outcome? In how far did it not work? At which point did you get stuck? – ImportanceOfBeingErnest Aug 23 '17 at 18:39
  • @ScottMermelstein Please can you explain it with an example, if my weight ranges from 2 to 15, how is the respective linewidth dependent on it ? – abcdef Aug 23 '17 at 18:40
  • If your weight ranges from 2 to 15, try `linewidth=str(weight/2)`. Then your linewidth will vary from 1 to 7.5. – Scott Mermelstein Aug 23 '17 at 18:42
  • @ScottMermelstein Thank you, but can you please show how to include "linewidth" in the code for each "id". – abcdef Aug 23 '17 at 18:58
  • Since you're talking about dataframes, am I correct that you're using pandas? (My solution assumed as much.) If so, I recommend you take out matplotlib-widget and numpy, since this has nothing to do with those, and add pandas. – Scott Mermelstein Aug 23 '17 at 19:18

1 Answers1

6

Based on the comments, you need to know a few things:

How to set the line width?

That's simple: linewidth=number. See https://matplotlib.org/examples/pylab_examples/set_and_get.html

How to take the weight and make it a significant width?

This depends on the range of your weight. If it's consistently between 2 and 15, I'd recommend simply dividing it by 2, i.e.:

linewidth=weight/2

If you find this aesthetically unpleasing, divide by a bigger number, though that would obviously reduce the number of linewidths you get.

How to get the weight out of df2?

Given the df2 you described and the code you showed, key is the id of df2. So you want:

df2[df2['id'] == key]['weight']

Putting it all together:

Replace your grp.plot line with the following:

grp.plot(linestyle="solid", 
         linewidth=df2[df2['id'] == key]['weight'] / 2.0, 
         x = "x", y = "y", ax = ax, label = key)

(All this is is your line with the entry for linewidth added in.)

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • Thank you, i tried as you suggested, but it shows me a key error KeyError: 'the label [6] is not in the [index]' – abcdef Aug 23 '17 at 19:42
  • Thank you, though it still gave me error **ValueError: could not convert string to float** so it worked after removing str() – abcdef Aug 23 '17 at 20:23