I am wondering if there could be any way to change the spacing between xticklabel (i.e. $\widetilde{M}) and the bottom of x-axis? In my case the spacing is too small so that the tilde above M (left bar) becomes invisible. BTW I am using pandas' plot function to generate the bar plot.
Since Pandas uses the Matplotlib library for all the plotting, you can change this setting through rcParams. First import:
from matplotlib import rcParams
and then (before plotting anything) change the padding above the xticks:
rcParams['xtick.major.pad'] = 20
Assuming you import matplotlib.pyplot as plt You can manipulate the pyplot object via the tick_params method and pad arg. E.g.:
plt.tick_params(pad=10)
Related
In Python, let's say I define two numpy arrays that I want to display in a 2D histogram:
import seaborn
import numpy as np
import matplotlib.pyplot as plt
num_samples = int(8e+4)
u0 = np.random.rand(num_samples)
E_incid = 90*np.random.rand(num_samples)+10
Now, with seaborn, I get this output:
However, since I need to switch to matplotlib, I want the same color scheme just in plt.hist2d(). What I did is to loop over all available colormaps (cf. this link)
and look which cmap resembles the one from seaborn, but I did not find a match. The closest match I found (with cmap='PuBu'):
Clearly, these two plots do not look similar; how I can force matplotlib to use the same colormap as seaborn? Thanks!
I'm plotting two data series with Pandas with seaborn imported. Ideally I would like the horizontal grid lines shared between both the left and the right y-axis, but I'm under the impression that this is hard to do.
As a compromise I would like to remove the grid lines all together. The following code however produces the horizontal gridlines for the secondary y-axis.
import pandas as pd
import numpy as np
import seaborn as sns
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'],grid=False)
You can take the Axes object out after plotting and perform .grid(False) on both axes.
# Gets the axes object out after plotting
ax = data.plot(...)
# Turns off grid on the left Axis.
ax.grid(False)
# Turns off grid on the secondary (right) Axis.
ax.right_ax.grid(False)
sns.set_style("whitegrid", {'axes.grid' : False})
Note that the style can be whichever valid one that you choose.
For a nice article on this, refer to this site.
The problem is with using the default pandas formatting (or whatever formatting you chose). Not sure how things work behind the scenes, but these parameters are trumping the formatting that you pass as in the plot function. You can see a list of them here in the mpl_style dictionary
In order to get around it, you can do this:
import pandas as pd
pd.options.display.mpl_style = 'default'
new_style = {'grid': False}
matplotlib.rc('axes', **new_style)
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'])
This feels like buggy behavior in Pandas, with not all of the keyword arguments getting passed to both Axes. But if you want to have the grid off by default in seaborn, you just need to call sns.set_style("dark"). You can also use sns.axes_style in a with statement if you only want to change the default for one figure.
You can just set:
sns.set_style("ticks")
It goes back to normal.
I know that I can set length of ticks with length parameter of AxesSubplot.tick_params() method.
How may I determine the actual length of major ticks of certain axis?
Please assume the length may be altered.
The default length of the ticks is determined by the xtick.major.size or ytick.major.size, or the xtick.minor.size or ytick.minor.size rcParams.
You may find out programmatically via
import matplotlib.pyplot as plt
print(plt.rcParams["xtick.major.size"])
This prints in the usual case 3.5 (size in points).
You may equally use those the rcParams to set the length
import matplotlib.pyplot as plt
plt.rcParams["xtick.major.size"] = 6
This will work in all cases where you create the axes yourself and have not (yet) changed the length e.g. via the tick_params method.
Otherwise, you can find out the tick length via
ax.xaxis.majorTicks[0].tick1line.get_markersize()
where ax is the axes in question.
I’d like to employ the reverse Spectral colormap ,
https://matplotlib.org/examples/color/colormaps_reference.html
for a lineplot.
This works fine with a hex bin plot::
color_map = plt.cm.Spectral_r
image = plt.hexbin(x,y,cmap=color_map)
but when I do
ax1.plot(x,y, cmp=color_map)
this gives me::
AttributeError: Unknown property cmap
Note, I just want to set the colormap and let matplotliob do the rest; i.e. I don't want to have a color=' argument in the .plot command.
You can have a look at this solution - the third variant is what you want:
https://stackoverflow.com/a/57227821/5972778
You need to know how many lines you're plotting in advance, as otherwise it doesn't know how to choose the colours from the range.
I think that seaborn's color_palette function is very convenient for this purpose. It can be used in a with statement to temporarily set the color cycle for a plot or set of plots.
For example:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
with sns.color_palette("Spectral", n_colors=10):
plt.plot(np.random.rand(5, 10))
You can use with any predefined matplotlib or seaborn colormap, or provide a custom sequence of colors.
I'm plotting two data series with Pandas with seaborn imported. Ideally I would like the horizontal grid lines shared between both the left and the right y-axis, but I'm under the impression that this is hard to do.
As a compromise I would like to remove the grid lines all together. The following code however produces the horizontal gridlines for the secondary y-axis.
import pandas as pd
import numpy as np
import seaborn as sns
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'],grid=False)
You can take the Axes object out after plotting and perform .grid(False) on both axes.
# Gets the axes object out after plotting
ax = data.plot(...)
# Turns off grid on the left Axis.
ax.grid(False)
# Turns off grid on the secondary (right) Axis.
ax.right_ax.grid(False)
sns.set_style("whitegrid", {'axes.grid' : False})
Note that the style can be whichever valid one that you choose.
For a nice article on this, refer to this site.
The problem is with using the default pandas formatting (or whatever formatting you chose). Not sure how things work behind the scenes, but these parameters are trumping the formatting that you pass as in the plot function. You can see a list of them here in the mpl_style dictionary
In order to get around it, you can do this:
import pandas as pd
pd.options.display.mpl_style = 'default'
new_style = {'grid': False}
matplotlib.rc('axes', **new_style)
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'])
This feels like buggy behavior in Pandas, with not all of the keyword arguments getting passed to both Axes. But if you want to have the grid off by default in seaborn, you just need to call sns.set_style("dark"). You can also use sns.axes_style in a with statement if you only want to change the default for one figure.
You can just set:
sns.set_style("ticks")
It goes back to normal.