hiding tick value on the y axis that are negative - matplotlib

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.

Related

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

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

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()

How to change colorbar's color (in some particular value interval)?

In matplotlib, I would like to change colorbar's color in some particular value interval. For example, I would like to change the seismic colorbar, to let the values between -0.5 and 0.5 turn white, how can I do this?
thank you very much
You basically need to create your own colormap that has the particular features you want. Of course it is possible to make use of existing colormaps when doing so.
Colormaps are always ranged between 0 and 1. This range will then be mapped to the data interval. So in order to create whites between -0.5 and 0.5 we need to know the range of data - let's say data goes from -1 to 1. We can then decide to have the lower (blues) part of the seismic map go from -1 to -0.5, then have white between -0.5 and +0.5 and finally the upper part of the seismic map (reds) from 0.5 to 1. In the language of a colormap this corresponds to the ranges [0,0.25], [0.25, 0.75] and [0.75,1]. We can then create a list, with the first and last 25% percent being the colors of the seismic map and the middle 50% white.
This list can be used to create a colormap, using matplotlib.colors.LinearSegmentedColormap.from_list("colormapname", listofcolors).
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
n=50
x = 0.5
lower = plt.cm.seismic(np.linspace(0, x, n))
white = plt.cm.seismic(np.ones(100)*0.5)
upper = plt.cm.seismic(np.linspace(1-x, 1, n))
colors = np.vstack((lower, white, upper))
tmap = matplotlib.colors.LinearSegmentedColormap.from_list('terrain_map_white', colors)
x = np.linspace(0,10)
X,Y = np.meshgrid(x,x)
z = np.sin(X) * np.cos(Y*0.4)
fig, ax = plt.subplots()
im = ax.imshow(z, cmap=tmap)
plt.colorbar(im)
plt.show()
For more general cases, you may need a color normalization (using matplotlib.colors.Normalize). See e.g. this example, where a certain color in the colormap is always fixed at a data value of 0, independent of the data range.

Matplotlib: how to control the plot interval of x axis?

I'm plotting the degree of freedom against the square error,:
plt.plot([1,2,3,4], [0.5,0.6,0.9,0.85],'-')
It will produce
The problem is that ,the x ax is has 0.5 interval, and does not make sense in this context. Because there is simply no 1.5 degree of freedom.
How can I make the x axis into [1,2,3,4,], instead of [1, 1.5, 2, ...]?
Just add directly the positions and the strings you want to put in the x axis. Using your example:
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [0.5,0.6,0.9,0.85]
plt.plot(x,y,'-')
plt.xticks(list(range(1,max(x)+1)),[str(i) for i in range(1,max(x)+1)])
plt.grid()
plt.show()
, which results in:
You have to set the XTick 1 to 4, by 1 1:1:4 like below
plot([1,2,3,4], [0.5,0.6,0.9,0.85],'-');
set(gca,'XTick',1:1:4);
or
p = plot([1,2,3,4], [0.5,0.6,0.9,0.85],'-');
set(p,'XTick',1:1:4);

Find value of axis in series

I'd like to know how to find the value of the X axis, where the X and Y axes cross. The result is to be displayed in a cell. VB is optional.
Example
Where the arrow points is the value I would like to get.
From where the 60 in the Y axis crosses the points, I'd like to get the Y axis.