python: python: scatter plot, The color of the Y axis makes the interval - matplotlib

I want to make intervals for each color, not to overlap on the Y axis.
How can I achieve this?
ax.set_title(df["me"][0],fontsize=20)
plt.xticks(rotation='vertical',fontsize=20)
plt.yticks(fontsize=20)
ax.set_ylabel(i,fontsize=40)
ax.grid(True)
plt.legend(loc='best',fontsize=20)
fig.savefig(i+".png")
plt.show()

Related

Seaborn - Change the X-Axis Range (Date field)

how can I change the x-axis so that I begin on January 1 2022? I don't want to set the other side of the bound. The aim here is to create a YTD chart. Thanks! (Data type for the x-axis field 'Date_reported' is a Dtype datetime64[ns]) (ps: does anyone know why my figsize statement isn't working? I'm aiming for the 15 by 8 siz but it doesn't seem to work.
sns.relplot(kind='line', data=df_Final, x='Date_reported', y='New_cases_Mov_avg',
hue='Continent', linewidth=1, ci=None)
sns.set_style("white")
sns.set_style('ticks')
plt.xlabel("Date Reported")
plt.ylabel("New Cases (Moving Average)")
plt.figure(figsize=(15,8))
You could define your figure and ax beforehand, set the figsize and then plot. Doing so, you have to go with lineplot instead of relplot.
ax.set_xlim will define the left boundary, fig.autofmt_xdate rotates the x labels.
fig, ax = plt.subplots(figsize=(15,8))
sns.lineplot(data=df_Final, x='Date_reported', y='New_cases_Mov_avg',
hue='Continent', linewidth=1, ci=None, ax=ax)
sns.set_style("white")
sns.set_style('ticks')
ax.set_xlabel("Date Reported")
ax.set_ylabel("New Cases (Moving Average)")
ax.set_xlim(datetime.date(2022, 1, 1))
fig.autofmt_xdate()

Increase the length of hline marker in matplotlib

I want to plot data using a constant, not too small, horizontal line for each value.
It seems the way to do it is with
x = np.arange(0, 10, 2)
y = [2,3,4,1,7]
plt.scatter(x, y, marker="_")
plt.legend(loc='Height')
plt.show()
but the horizontal lines are too small. Can they be customized to some greater length, at least a length similar to thewidth of a bar plot? Thx.
Do you mean that you want to increase marker size?
plt.scatter(x, y, marker="_", s=400)
s=1000

Creating a 3 category binned colormap for continuous variable for matplotlib plot?

I have two measurements x & y and one calculation f(x, y) where f(x, y) can be broken into 3 categories: Acceptable(>1.2), At Risk (1 < x <=1.2), and Not Acceptable (<=1). I was wondering what is the best way to bin and plot this where f(x,y) is the colormap for a y v x scatter plot.
Thanks!
I'm not sure if this is exactly what you are after but sounds like you might be interested in pd.cut

hiding tick value on the y axis that are negative

I am trying to hide any value on the y axis that is less than 0. I saw that to hide labels on the y axis I have to use something like this:
make_invisible = True
ax4.set_yticks(minor_ticks)
if (make_invisible):
yticks=ax4.yaxis.get_major_ticks()
yticks[0].label1.set_visible(False)
How can I tweak this so that if the ytick lable is negative it will be hidden?
You can use the set_xticks() method to simply set those ticks that you want on the x axis.
import matplotlib.pyplot as plt
plt.figure(figsize=(7,3))
plt.plot([-2,-1,0,1,2],[4,6,2,7,1])
ticks = [tick for tick in plt.gca().get_xticks() if tick >=0]
plt.gca().set_xticks(ticks)
plt.show()
Replacing every x by a y will give you the according behaviour on the y axis.

matplotlib: left yaxis and right yaxis to have different units

I'm plotting curves in Kelvin.
I would like to have the left yaxis to show units in Kelvin and the right yaxis to show them in Celsius, and both rounded to the closest integer (so the ticks are not aligned, as TempK=TempC+273.15)
fig=plt.figure
figure=fig.add_subplot(111)
figure.plot(xpos, tos, color='blue')
I should not use twinx() as it allows superimposing curves with two different scales, which is not my case (only the right axis has to be changed, not the curves).
I found the following solution:
fig=plt.figure
figure=fig.add_subplot(111)
figure.plot(xpos, tos, color='blue')
... plot other curves if necessary
... and once all data are plot, one can create a new axis
y1, y2=figure.get_ylim()
x1, x2=figure.get_xlim()
ax2=figure.twinx()
ax2.set_ylim(y1-273.15, y2-273.15)
ax2.set_yticks( range(int(y1-273.15), int(y2-273.15), 2) )
ax2.set_ylabel('Celsius')
ax2.set_xlim(x1, x2)
figure.set_ylabel('Surface Temperature (K)')
Do not forget to set the twinx axis xaxis!