Dataframe.plot() not working when ax is defined - pandas

I am trying to emulate the span selector for the data I have according to the example shown here (https://matplotlib.org/examples/widgets/span_selector.html).
However, my data is in a dataframe & not an array.
When I plot the data by itself with the using the code below
input_month='2017-06'
plt.close('all')
KPI_ue_data.loc[input_month].plot(x='Order_Type', y='#_Days_#_Post_stream')
plt.show()
the data chart is shown perfectly.
However when i am trying to put this into a subplot with the code below (only first two lines are added & ax=ax in the plot line), nothing shows up. I get no error either!!! can anyone help?
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(211, facecolor='#FFFFCC')
input_month='2017-06'
plt.close('all')
KPI_ue_data.loc[input_month].plot(x='Order_Type', y='#_Days_#_Post_stream',ax=ax)
plt.show()

I usually just set x, y from the dataframe and use ax.plot(x, y). For your code, it should look something like this:
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(211, facecolor='#FFFFCC')
input_month='2017-06'
#plt.close('all')
x = KPI_ue_data.loc[(input_month), 'Order_Type']
y = KPI_ue_data.loc[(input_month), '#_Days_#_Post_stream']
ax.plot(x, y)
plt.show()

Related

Matplotlib doesn't show both datasets points on the figure when I want to create scatter plot with

I'm sure that I've done all things right but in the end the result I got is a sccatter plot that only shows the second datasets data.
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(train["ENGINESIZE"], train["CO2EMISSIONS"], color = "green")
ax1.scatter(test["ENGINESIZE"], test["CO2EMISSIONS"], color = "red")
plt.xlabel("Engine Size")
plt.ylabel("Emission")
plt.show()
Here You can see what's going on in my output in link below.
It shows only red data(test data) in the output.
Where is the "output link below", please? For now I can only imagine what you are describing.
Also it helps if both plots have the same axis. That is, both have the same x-axis and then they can vary on their y-axis.
If so:
fig, ax = plt.subplots()
df.plot(kind = 'scatter', x= train["ENGINESIZE"], y = train["CO2EMISSIONS"], color = {'g'}, ax = ax)
df.plot(kind = 'scatter', x= test["ENGINESIZE"], y = test["CO2EMISSIONS"], color = {'r'}, ax = ax)
plt.xlabel()

y and x axis subplots matplotlib

A quite basic question about ticks' labels for x and y-axis. According to this code
fig, axes = plt.subplots(6,12, figsize=(50, 24), constrained_layout=True, sharex=True , sharey=True)
fig.subplots_adjust(hspace = .5, wspace=.5)
custom_xlim = (-1, 1)
custom_ylim = (-0.2,0.2)
for i in range(72):
x_data = ctheta[i]
y_data = phi[i]
y_err = err_phi[i]
ax = fig.add_subplot(6, 12, i+1)
ax.plot(x_data_new, bspl(x_data_new))
ax.axis('off')
ax.errorbar(x_data,y_data, yerr=y_err, fmt="o")
ax.set_xlim(custom_xlim)
ax.set_ylim(custom_ylim)
I get the following output:
With y labels for plots on the first column and x labels for theone along the last line, although I call them off.
Any idea?
As #BigBen wrote in their comment, your issue is caused by you adding axes to your figure twice, once via fig, axes = plt.subplots() and then once again within your loop via fig.add_subplot(). As a result, the first set of axes is still visible even after you applied .axis('off') to the second set.
Instead of the latter, you could change your loop to:
for i in range(6):
for j in range(12):
ax = axes[i,j] # these are the axes created via plt.subplots(6,12,...)
ax.axis('off')
# … your other code here

How to view data of particular point(s) in a scatterplot in Matplotlib?

I am using the following snippet of code to make a scatterplot using seaborn:
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize = (15,7))
sns.set()
plt.figure(figsize = (15,7))
sns.scatterplot(data = analysis_df, x = 'nota_percoftotal', y = 'post-pre')
plt.show()
This is the figure that I get:
As you can see, there is a stream of data points lying on the downwards sloping line. My question is, how can I know what these data points are in my dataset? My dataset looks something like this:
uni_const_code nota_percoftotal post-pre
1|9|95 34.337349 -15.637197
1|9|96 43.684211 -52.935894
1|9|97 42.857143 -14.455647
The column uni_const_code is the key in the dataset.
Edit 1: I used mplcursors making the following modifications:
fig, ax = plt.subplots()
x = analysis_df['nota_percoftotal']
y = analysis_df['post-pre']
line, = ax.plot(x, y, "to")
mplcursors.cursor(ax).connect("add", lambda sel: sel.annotation.set_text(labels[sel.target.index]))
plt.show()
But I still don't get what I want. The output is just a scattergram like before, I don't get anything even when I hover/click on the scatter dots.

Draw various plots in one figure

The image below shows, what i want, 3 different plots in one execution but using a function
enter image description here
enter image description here
I used the following code:
def box_hist_plot(data):
sns.set()
ax, fig = plt.subplots(1,3, figsize=(20,5))
sns.boxplot(x=data, linewidth=2.5, ax=fig[0])
plt.hist(x=data, bins=50, density=True, ax = fig[1])
sns.violinplot(x = data, ax=fig[2])
and i got this error:
inner() got multiple values for argument 'ax'
Besides the fact that you should not call a Figure object ax and an array of Axes object fig, your problem comes from the line plt.hist(...,ax=...). plt.hist() should not take an ax= parameter, but is meant to act on the "current" axes. If you want to specify which Axes you want to plot, you should use Axes.hist().
def box_hist_plot(data):
sns.set()
fig, axs = plt.subplots(1,3, figsize=(20,5))
sns.boxplot(x=data, linewidth=2.5, ax=axs[0])
axs[1].hist(x=data, bins=50, density=True)
sns.violinplot(x = data, ax=axs[2])

How to force a graph within a grid in matplotlib?

I have a grid that looks like this
fig = plt.figure()
ax = fig.gca()
ax.set_xticks(numpy.arange(-5,6,1))
ax.set_yticks(numpy.arange(-5,6,1))
plt.grid(True)
When plotting an exponential function, obviously the function's values grow larger than the grid very quickly, and my grid ticks get distorted. I want the grid to be fixed, and only that part of the function to be graphed which fits inside of the grid. Is this possible?
Thanks in advance.
You can set the limits of the axes:
fig = plt.figure()
ax = fig.gca()
# Exponential plot:
x = linspace(-5, 5, 100)
y = power(2, x)
ax.plot(x, y)
ax.set_xticks(numpy.arange(-5,6,1))
ax.set_yticks(numpy.arange(-5,6,1))
ax.set_xlim(-5, 6)
ax.set_ylim(-5, 6)
plt.grid(True)