How to represent a single point on a matlplotlib plot [duplicate] - python

This question already has answers here:
How to plot one single data point?
(4 answers)
Closed 4 years ago.
I have a graph which represents the sentiments of the values in a column of pandas dataframe. Given a sentence, I want to highlight the corresponding sentiment value on the graph/plot. I am looking for an output similar to the image below:

You can use pyplot.scatter and pass single values in for x and y. Here's an example:
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure()
x_data = np.linspace(0,3.5,100)
y_data = [-x * (x - 3.2) for x in x_data]
plt.plot(x_data, y_data, color='#0000ff', zorder=0)
x_point = x_data[20]
y_point = y_data[20]
plt.scatter(x_point, y_point, color='#ff0000', zorder=1)
Produces:

Related

Polynomial function in matplotlib displaying multiple lines [duplicate]

This question already has an answer here:
Too many lines and curves on the polynomial graph
(1 answer)
Closed 9 months ago.
I am experiencing the same issue that is asked in this question: Too many lines and curves on the polynomial graph
The solution for that issue seems to be sorting the points based on the x axis. In my case im pretty sure my data is already sorted as I am placing my x array into the plot like so:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.array([0,0,0,0,0,0,26,0,0,0,0,0,0,0,0,0,214,67,225,250,0,0,0,94,0,0,1366,137])
y = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,0,0,0,0,0,0,4,0])
fig3, ax3 = plt.subplots()
ax3.scatter(x, y)
ax3.plot(x, 0 + 0.004*x + 1.63e-06*(x**2), label='squared')
ax3.legend()
plt.show()
I would like to plot just the quadratic line:
Your example does indeed have unsorted data in the x axis, and the solution is just as in the question you linked:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.array([0,0,0,0,0,0,26,0,0,0,0,0,0,0,0,0,214,67,225,250,0,0,0,94,0,0,1366,137])
y = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,5,0,0,0,0,0,0,4,0])
order = np.argsort(x)
fig3, ax3 = plt.subplots()
ax3.scatter(x[order], y[order])
ax3.plot(x[order], 0 + 0.004*x[order] + 1.63e-06*(x[order]**2), label='squared')
ax3.legend()
plt.show()

Proper visualization of the label name [duplicate]

This question already has answers here:
Rotate axis text in python matplotlib
(13 answers)
How to rotate x-axis tick labels in a pandas plot
(5 answers)
Closed 1 year ago.
How can I properly structure the label name of the generated graph? The code used in generating the graph is written below:
from sklearn.feature_selection import mutual_info_classif
plt.figure(figsize=(20,5))
feat_import = pd.Series(importance, new_data.columns[0:len(new_data.columns)-2])
plt.plot(feat_import, 'r')
plt.title('Best Feature Selection')
plt.ylabel('Importance')
plt.xlabel('Available features')
plt.legend([ 'importance to dependent variable'], loc='upper right')
The generated result is this:
I underlined with green color where I am having the issue.
One of the best ways will be to show the label name vertically rather than the horizontal display shown above. Please, how can I achieve that?
Rotating the label names of the horizontal axis can be done via:
plt.xticks(rotation = 90) # Rotates X-Axis Ticks by 90-degrees
Minimal example:
import matplotlib.pyplot as plt
import pandas as pd
plt.figure(figsize=(6,6))
feat_import = pd.Series(data=range(10), index=['abc' * (i+1) for i in range(10)])
plt.plot(feat_import, 'r')
plt.xticks(rotation=90)
plt.show()

Matplotlib: filling the area under the curve between two x-values [duplicate]

This question already has answers here:
fill_between with matplotlib and a where condition of two lists
(1 answer)
Matplotlib fill_between edge effect with `where` argument
(1 answer)
Fill between x and baseline x position in Matplotlib
(1 answer)
How to avoid gaps with matplotlib.fill_between and where
(1 answer)
Closed 1 year ago.
I'm plotting a blackbody curve and would like to fill in the area under the curve in the range of between 3 and 5 micron. However, I'm not sure how to use the fill_between or fill_betweenx plt commands here
import numpy as np
import matplotlib.pyplot as plt
from astropy import units as u
from astropy.modeling import models
from astropy.modeling.models import BlackBody
from astropy.visualization import quantity_support
bb = BlackBody(temperature=308.15*u.K)
wav = np.arange(1.0, 50.0) * u.micron
flux = bb(wav)
with quantity_support():
plt.figure()
plt.plot(wav, flux, lw=4.0)
plt.fill_between(wav,flux, min(flux), color = 'red')
plt.show()
This plots a fill under the whole curve, but only the 3-5micron part is desired to be filled.
example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 100) # Sample data.
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
fig, ax = plt.subplots() # Create a figure and an axes.
print(x)
ax.plot(x, x, label='linear') # Plot some data on the axes.
ax.set_xlabel('x label') # Add an x-label to the axes.
ax.set_ylabel('y label') # Add a y-label to the axes.
ax.set_title("Simple Plot") # Add a title to the axes.
ax.legend() # Add a legend.
plt.fill_between(x[:5],x[:5])
plt.show()
You can change the value 5 and play with it, you'll understand quickly. first parameter is Y positions , second is X positions.
fill_betweenx is just the same, but it will fill the other way around.
edit: As said in comments, it is better to use plt.fill_between(x,x, where = (x>0)&(x<0.2)). Both works, second solution is more explicit.

To change values across x & y axis in a plotting [duplicate]

This question already has answers here:
How to set ticks on Fixed Position , matplotlib
(2 answers)
How to force integer tick labels
(4 answers)
How to force matplotlib to show values on x-axis as integers
(2 answers)
In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]
(1 answer)
Closed 1 year ago.
I want to change values across the x- & y-axes in a plot.
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['figure.figsize'] = 7,6
x = range(1,20)
y = list(np.random.rand(19) * 100)
fig = plt.figure()
ax = fig.add_axes([0.01,0.1,1,1])
ax.plot(x,y,marker = 'o')
I want to change x-axis values that are appearing as decimals. Instead, I want to customize it and show values like 2,4,6 .. 18.
Any suggestions will be a kind help.
Thank you

Question about mapping x-axis values in matplotlib [duplicate]

This question already has answers here:
Plot some numbers in x axis
(3 answers)
plot with custom text for x axis points
(3 answers)
Force python axis to show specific numbers
(2 answers)
Closed 1 year ago.
I am starting to use the matplotlib library to generate simple graphs. In one of my tests something happens to me that does not allow me to obtain the graph that I expect. Coming to the point, I have two value arrays (circle and g) and I would like to join each of their values. The problem comes when I input the x-axis values. I would like only the values of my array g to be on the x axis, but the following happens:
In my code I have the following:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
circle = [0.002,0.013,0.035]
g = [5,25,50]
ax.scatter(g[0], circle[0], color = 'g', marker = "o")
x = np.array([0,1,2,3])
my_xticks = [0,5,25,50]
plt.xticks(x, my_xticks)
plt.show()
Could someone help me fix this?
The problem was that when calling plt.xticks(), you passed [0, 1, 2, 3] as the x-tick values, and you passed the tick values you really wanted as the tick labels. Instead, pass the tick values you want as the first argument, and omit the second argument. The tick labels will be strings of the tick values by default.
import matplotlib.pyplot as plt
circle = [0.002, 0.013, 0.035]
g = [5, 25, 50]
my_xticks = [0] + g
fig, ax = plt.subplots()
ax.scatter(g, circle, color='g', marker='o')
plt.xticks(my_xticks)
plt.show()

Categories