#en jupyter y en colab da eror , en azure no
import graphing
# Show a graph of the result
# Don't worry about how this works for now
graphing.scatter_2D(dataset,label_x="harness_size",
label_y="boot_size",
trendline=lambda x: fitted_model.params[1] * x + fitted_model.params[0])
-______________________________
4 import graphing
6 # Show a graph of the result
7 # Don't worry about how this works for now
----> 8 graphing.scatter_2D(dataset, label_x="harness_size",
9 label_y="boot_size",
10 trendline=lambda x: fitted_model.params[1] * x + fitted_model.params[0]
11 )
AttributeError: module 'graphing' has no attribute 'scatter_2D'
a cambiar por :
import plotly.express as px
px.scatter(dataset,x="harness_size",y="boot_size",title='Botas y arneses')
Related
I am generating a graph using pandas. When I run my script locally, I get this error:
AttributeError: module 'pandas.plotting' has no attribute '_matplotlib'
I installed pandas for both my user and system. The operating system is WSL2 Ubuntu 20.04.
This is the relevant part of my code:
import pandas as pd
import matplotlib.pyplot as plt
def plot_multi(data, cols=None, spacing=.1, **kwargs):
from pandas import plotting
# Get default color style from pandas - can be changed to any other color list
if cols is None: cols = data.columns
if len(cols) == 0: return
colors = getattr(getattr(plotting, '_matplotlib').style, '_get_standard_colors')(num_colors=len(cols))
# First axis
print(data.loc[:, cols[0]])
ax = data.loc[:, cols[0]].plot(label=cols[0], color=colors[0], **kwargs)
ax.set_ylabel(ylabel=cols[0])
lines, labels = ax.get_legend_handles_labels()
for n in range(1, len(cols)):
# Multiple y-axes
ax_new = ax.twinx()
ax_new.spines['right'].set_position(('axes', 1 + spacing * (n - 1)))
data.loc[:, cols[n]].plot(ax=ax_new, label=cols[n], color=colors[n % len(colors)], **kwargs)
ax_new.set_ylabel(ylabel=cols[n])
# Proper legend position
line, label = ax_new.get_legend_handles_labels()
lines += line
labels += label
ax.legend(lines, labels, loc=0)
return ax
This worked on a University lab machine. Not sure why it's not working locally.
Whenever I try to plot something with matplotlib, I get the following error:
File "C:\Users\username\AppData\Local\Programs\Python\Python37-32\Lib\tkinter\__init__.py", line 2018, in __init__
baseName = os.path.basename(sys.argv[0])
builtins.IndexError: list index out of range
For example, i've tried the following code:
import matplotlib.pyplot as plt
import numpy as np
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
I have the latest version of matplotlib, please help.
Thank you
You need to import numpy.
import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
I have made a violinplot and want to rename the x-labels .
ax = sns.violinplot(x="Week_Number", y="Ammonia", data=Res)
this is the output:
And What I want to Have is , rather than 1 I want Week 1 , than for 44 i Want Week 2 until Week 10 for 52.
Thanks Everyone
You're looking to set_xticklabels property (doc). To apply this function, you need to have the axis. There is the same for y labels with set_yticklabels.
Here the code is adapted from Seaborn examples:
# Import modules
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# Create your list of labels
week_list = ["Week_" + str(i) for i in range(1, 10)]
# ['Week_1', 'Week_2', 'Week_3', 'Week_4', 'Week_5', 'Week_6', 'Week_7', 'Week_8', 'Week_9']
fig = plt.figure() # Create a new figure for getting axis
ax = fig.add_subplot(111) # Get the axis
# Create a random dataset across several variables
rs = np.random.RandomState(0)
n, p = 40, 8
d = rs.normal(0, 2, (n, p))
d += np.log(np.arange(1, p + 1)) * -5 + 10
# Use cubehelix to get a custom sequential palette
pal = sns.cubehelix_palette(p, rot=-.5, dark=.3)
# Show each distribution with both violins and points
sns.violinplot(data=d, palette=pal, inner="points")
week_list = ["Week_" + str(i) for i in range(1,10)]
# Set the x labels
ax.set_xticklabels(week_list)
# Show figure
plt.show()
This question already has an answer here:
Matplotlib fill area under curve between two x values only
(1 answer)
Closed 4 years ago.
I am trying to produce a plot wth a fill only in an interval
I can set one boundary to the interval, but using 2 gives an error message:
This piece of code works
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, 500)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
plt.plot(x,y)
plt.fill_between(x,0, y,where=x>0.25)
plt.show()
Obtained figure
Tis piece of code gives an error
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, 500)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
plt.plot(x,y)
plt.fill_between(x,0, y,where=0.5>x>0.25)
plt.show()
----> 7 plt.fill_between(x,0, y,where=0.5>x>0.25)
8 plt.show()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I could not figure out how to solve this
You can use Boolean "and":
plt.fill_between(x,0, y,where=(0.5>x) & (x>0.25))
I'm trying to define a heaviside function in python but am getting a weird error. I'm not sure what 'type' refers to because 0 can be nothing but an integer. Please advise
#Part A - Plot function against values of variable x
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as quad
import math
#make heaviside "theta" function
x = int
def heaviside (x):
if (x >= 0):
return 1
else:
return 0
#plot
x = int
y = heaviside(x)*[1-heaviside(x-1)]
plt.plot(x, y)
When you declare x=int you are equating x to the datatype int, not to an instance of that datatype.
You can run something like this code to see it.
>>> x = int
>>> x(4.5)
4
is equivalent to running
>>>int(4.5)
4
If you pass a numeric value for x instead, like x=4 then you won't have this error.
The code below doesn't raise this error anymore. The resulting plot doesn't show much; but there is no TypeError: unorderable types: type() >= int()
#Part A - Plot function against values of variable x
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as quad
import math
#make heaviside "theta" function
def heaviside (x):
if (x >= 0):
return 1
else:
return 0
#plot
x = 5
y = heaviside(x)*[1-heaviside(x-1)]
plt.plot(x, y)
You are setting x incorrectly. If you want x to be an integer, simply use the assignment operator like below.
x = 5