'Python define a function to plot the column
I want to create a function where it will plot the CO2_emission and another indicator (which is selected by user).
def CO2_indicator(driver, df = CO2_driver):
# create plot
fig = px.scatter(
df,
x = df['CO2_emission_pc'],
y = df[driver],
hover_name = 'country',
)
fig.update_layout(
yaxis_title = driver,
xaxis_title = 'CO2_emission_pc',
title = 'Relationship between CO2 emission per capita and ' + driver,
title_x = 0.5,
margin = {'l' : 0, 'r' : 0}
)
return fig
i run the function :
CO2_indicator(forest_area, df = CO2_driver ) #(forest_area is the indicator)
[enter image description here][2]
The wrong is in the orange part, it cant subset the column forest_area in the dataframe. It must be df['forest_area'] (i mean put it into quotation) How can I solve this? Thank you. [1]: https://i.stack.imgur.com/ENW19.png [2]: https://i.stack.imgur.com/APYU5.png
Solution 1:[1]
see this image please and try the following code , I hope that help you :
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("FuelConsumption.csv") #assum this DataFram you use
cdf =df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_CITY','FUELCONSUMPTION_HWY','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
CO2EMISSIONS = cdf.CO2EMISSIONS
def func (user_selected):
plt.scatter(user_selected, CO2EMISSIONS, color='blue')
plt.xlabel("CO2EMISSIONS")
plt.ylabel("{}".format(user_selected.name))
plt.show()
then call function :
func(cdf.CYLINDERS)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Al Fa |
