Plotting with Pandas#
easychart
natively supports pd.Series
and pd.DataFrame
objects. Each pd.Series
or pd.DataFrame
is treated as a single series, and each row will be handled as a data point. Numeric values from a pd.Index
will be used as the x
coordinates, although you can override this.
import pandas as pd
import easychart
data = pd.Series([(x - 20) ** 2 + 3 for x in range(10, 30)], index=range(10, 30))
chart = easychart.new()
chart.plot(data)
chart
Here’s an example from a pd.DataFrame
.
import easychart
import pandas as pd
grades = pd.DataFrame(
[[68, 87], [71, 85]], columns=["min", "max"], index=["male", "female"]
)
chart = easychart.new("columnrange")
chart.categories = grades.index
chart.plot(grades, name="min-max range of grades")
chart
Important
The number of columns of the pd.DataFrame
must be appropriate for the series type.
- For example:
a
line
series requires an(x, y)
value for each point, so 2 columns (or 1 column and thepd.Index
) are expecteda
columnrange
series requires an(x, min, max)
value for each point, so 3 columns (or 2 columns and thepd.Index
) are expecteda
bubble
series requires an(x, y, z)
value for each point, so 3 columns (or 2 columns and thepd.Index
) are expected
import math import pandas as pd import easychart numbers = pd.DataFrame([[x, math.cos(x)] for x in range(-100, 100)]) chart = easychart.new("scatter") chart.plot(numbers.values) # 2 columns only (no index) chart
import math import pandas as pd import easychart numbers = pd.DataFrame( [[math.cos(x), math.sin(x)] for x in range(-10, 10)], index=range(-10, 10) ) chart = easychart.new("bubble") chart.plot(numbers) # index + 2 columns chart
You can override the index of the data by explicitly passing an index
argument. In the below example, we override the x coordinates to the range 100 to 130.
import pandas as pd
import easychart
data = pd.Series([(x - 20) ** 2 + 3 for x in range(10, 30)], index=range(10, 30))
chart = easychart.new()
chart.plot(data, index=range(100, 130))
chart
Alternatively, you can drop the index by calling the values
property of the pandas objects.
Important
This is particularly relevant for scatter plots drawn from two columns of a pd.DataFrame
objects.
import math
import pandas as pd
import easychart
numbers = pd.DataFrame([[x, math.cos(x)] for x in range(-100, 100)])
chart = easychart.new("scatter")
chart.plot(numbers.values) # 2 columns only (no index)
chart
In the below example, we drop the index, and the x coordinates default to the range [0...n]
.
import pandas as pd
import easychart
data = pd.Series([(x - 20) ** 2 + 3 for x in range(10, 30)], index=range(10, 30))
chart = easychart.new()
chart.plot(data.values)
chart