3

I have the following code for plotting two variables 'field_size' and 'field_mean_LAI':

plt.figure(figsize=(20,10))
plt.scatter(df.field_size, df.field_lai_mean)
plt.title("Field Size and LAI Plot")
plt.xlabel("Field Size")
plt.ylabel("Mean LAI")
plt.show()

The outcome is a scatter plot: "enter image description here

How can I configure the x and y intervals and change color of the plot?? I am very beginner in python plotting.

tahmid0945
  • 95
  • 2
  • 11
  • 3
    Could you describe a bit more precisely what is needed? What do you mean by "configure the x and y intervals and change color of the plot"? – JohanC Sep 07 '20 at 10:46
  • 1
    Maybe some of the 2D plots at [this seaborn tutorial](https://seaborn.pydata.org/tutorial/distributions.html) could be interesting? – JohanC Sep 07 '20 at 10:48
  • 1
    Hi, it would be nice to have clear information about the expected output. You can change the color of the points by providing `c`or `color` within plt.scatter. Also, formatting related question have been asked before. Here is one example. https://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib – Grayrigel Sep 07 '20 at 10:53
  • Thanks for the suggestions. Got the solution. Posting as an answer. – tahmid0945 Sep 07 '20 at 11:15

2 Answers2

3

So I modified my code like below:

plt.figure(figsize=(20,10))
plt.scatter(df.field_size, df.field_lai_mean)
plt.title("Field Size and LAI Plot")
plt.xlabel("Field Size")
plt.ylabel("Mean LAI")

plt.xticks(np.arange(0.00000,0.00013,step=0.000008))
plt.yticks(np.arange(0,8.5,step=0.5))

plt.show()

Now I have a plot like this:

enter image description here

Just defined the xticks and yticks functions and it is done smoothly! :)

tahmid0945
  • 95
  • 2
  • 11
1

To change the color of plot points, you can use color attribute (see below).

To set limits of both axes, you can pass xlim and ylim parameters while creating the subplot.

Note also that I passed here also rot parameter, to set x labels rotation.

And to configure x and y intervals, you can use e.g. MultipleLocator, for both major and minor ticks. There are other locators too, search the Web for details.

Additional element which you can set is also the grid (like I did).

So you can change your code to:

import matplotlib.pyplot as plt
import matplotlib.ticker as tck

fig = plt.figure(figsize=(10,5))
ax = plt.subplot(xlim=(-0.000003, 0.000123), ylim=(-0.5, 9))
df.plot.scatter(x='field_size', y='field_lai_mean', rot=30, color='r', ax=ax)
plt.title("Field Size and LAI Plot")
plt.xlabel("Field Size")
plt.ylabel("Mean LAI")
ax = plt.gca()
ax.yaxis.set_major_locator(tck.MultipleLocator(2))
ax.yaxis.set_minor_locator(tck.MultipleLocator(0.4))
ax.xaxis.set_major_locator(tck.MultipleLocator(0.00001))
ax.xaxis.set_minor_locator(tck.MultipleLocator(0.0000025))
ax.grid()
plt.show()

Of course, adjust passed values to your needs.

For a very limited set of points, I got the following picture:

enter image description here

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41