How to get rid of all ylabels of all subplots.[matplotlib] [duplicate] - python

This question already has an answer here:
Setting axis labels for histogram pandas
(1 answer)
Closed 2 years ago.
I would like to know how to get rid of all labels of all subplots. I have a dataframe consisting of 37 columns. Then, to make histograms for them, I wrote this code.
p_variables.plot.hist(subplots=True,layout=(5,8),figsize=(20,20),sharex=False,ylabel="")
plt.show()
I expected that all of ylabels of subplots were invisible by setting ylabel="". However, they do not disappear. Could someone give me idea how to solve this?
The output is below. I would like to get rid of Frequency labels.

You'll need to iterate over the returned axes and set the ylabel to "" explicitly.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(40, 5), columns=list("ABCDE"))
axes = df.plot.hist(subplots=True,layout=(5,8),figsize=(20,20),sharex=False)
for ax in axes.flatten():
ax.set_ylabel("")
plt.show()

Related

X axis for plt plot python is cluttered together [duplicate]

This question already has answers here:
Matplotlib showing x-tick labels overlapping
(3 answers)
Closed 11 months ago.
plt.figure(figsize=(4,4))
aapl_data.plot.line(x='Date',y='Adj Close',title='test')
plt.ylabel('Adj Close')plt.show()
How do i declutter the X axis. I tried using figsize in the code but it does not do anything
Better show the whole code. Since I'm not sure if you have such a string: ax = plt.axes()
ax.xaxis.set_major_locator(mdates.DayLocator(interval = 3))
Try to formate the date
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b'))
There can be two solutions to this problem.
Increasing the width of the window. This can be achieved by:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(40,4))
fig.add_subplot(1,1,1).plot([1,2,3], [1,2,3])
plt.show()
Making the labels vertical rather than horizontal. This can be done by:
plt.xticks(range(10), rotation='vertical')

Pandas plot: Is it possible draw other thing than the column names as label? [duplicate]

This question already has answers here:
Modify the legend of pandas bar plot
(3 answers)
Closed 1 year ago.
I'm using this dataframe: https://www.kaggle.com/fivethirtyeight/fivethirtyeight-fandango-dataset it has several columns that I want to plot, tehy are ['RT_norm', 'RT_user_norm', 'Metacritic_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Stars']
When I do any kind of plot with Pandas, the labels are the column labels (Duh!)
df.head().plot.bar(x='FILM', y=marc, figsize=(10,8), grid=True)
plt.title('Calificaciones de Películas por Sitio')
plt.ylabel('Calificación')
plt.xlabel('Película')
Is there any chance I can change the labels to be something else? I dunno... instead of RT_norm I'd want Rotten Tomatoes Normalized, or the only correct answer is to change the column names in the dataframe? I tried using yticks and ylabel parameters, but they just don't work as I want.
I think you want to change the legend labels using plt.legend(labels=..) :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'FILM':range(100),
'y1':np.random.uniform(0,1,100),
'y2':np.random.uniform(0,1,100)})
df.head().plot.bar(x='FILM', y=['y1','y2'], figsize=(10,8), grid=True)
plt.legend(labels=['bar1','bar2'])

Matplotlib: make multiple-panel figure of existing figures? [duplicate]

This question already has answers here:
plot several image files in matplotlib subplots
(2 answers)
Closed 1 year ago.
I am producing a lot of figures with Matplotlib.pyplot (spatial data) and saving them as png's. I would like to be able to first make the figures (in loops), and then choose a few to put together in a multiple-panel figure, using Matplotlib.
I suppose this would mean re-opening the existing png's, and then putting them together using pyplot.subplots(), but I can't figure out a way to do it.
Does anybody have an idea?
Thanks!
Here's an example of what I think you mean:
import matplotlib.pyplot as plt
from matplotlib import image
import numpy as np
# initialise grid of axes
fig, axes = plt.subplots(2,2)
axes = axes.ravel()
# create fake data
img = [
'01-img.png',
'02-img.png',
'03-img.png',
'04-img.png',
]
# iterate over axes
for i, ax in enumerate(axes):
im = image.imread(img[i])
ax.imshow(im)
plt.show()
Use image.imread to load the image into a plottable form, then use ax.imshow to plot the pixels on the axis

How to plot y-axis to the opposite side? [duplicate]

This question already has answers here:
matplotlib y-axis label on right side
(4 answers)
Closed 2 years ago.
I have this chart below:
I would want the y-axis for the lower subplot to be plotted to the opposite side since that would make more sense. Is there a method for this? The ax.invert_yaxis() simply inverts the labels.
Note: For the curious, I simply used .invert_xaxis() to plot inverted bars.
I guess, what you are looking for is
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
of an axis object.
So with #meTchaikovsky's MVE code, you'll get
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(1,10,10)
y0 = np.random.randint(0,30,size=10)
fig,ax = plt.subplots(nrows=2,ncols=1,figsize=(8,6))
ax[1].set_xlim(0,30)
ax[0].barh(x,y0,color='violet')
ax[0].set_ylabel("Y-Axis")
ax[1].set_xlim(30,0)
ax[1].barh(x,y0,color='deepskyblue')
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
ax[1].set_ylabel("Y-Axis")
plt.show()

Why does plt.figure(figsize) render different results when plotting pd.DataFrame and pd.Series? [duplicate]

This question already has answers here:
Inconsistency when setting figure size using pandas plot method
(2 answers)
Closed 4 years ago.
In the two snippets below, where the only difference seems to be the datasource type (pd.Series vs pd.DataFrame), does plt.figure(num=None, figsize=(12, 3), dpi=80) have an effect in one case but not in the other when using pd.DataFrame.plot?
Snippet 1 - Adjusting plot size when data is a pandas Series
# Imports
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# data
np.random.seed(123)
df = pd.Series(np.random.randn(10000),index=pd.date_range('1/1/2000', periods=10000)).cumsum()
print(type(df))
# plot
plt.figure(num=None, figsize=(12, 3), dpi=80)
ax = df.plot()
plt.show()
Output 1
Snippet 2 - Now the data source is a pandas Dataframe
# imports
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# data
np.random.seed(123)
dfx = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
dfy = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
df = pd.concat([dfx, dfy], axis = 1)
print(type(df))
# plot
plt.figure(num=None, figsize=(12, 3), dpi=80)
ax = df.plot()
plt.show()
The only difference here seems to be the type of the datasource. Why would that have something to say for the matplotlib output?
It seems that pd.Dataframe.plot() works a bit differently from pd.Series.plot(). Since the dataframe might have any number of columns, which might require subplots, different axes, etc., Pandas defaults to creating a new figure. The way around this is to feed the arguments directly to the plot call, ie, df.plot(figsize=(12, 3)) (dpi isn't accepted as a keyword-argument, unfortunately). You can read more about in this great answer:
In the first case, you create a matplotlib figure via fig =
plt.figure(figsize=(10,4)) and then plot a single column DataFrame.
Now the internal logic of pandas plot function is to check if there is
already a figure present in the matplotlib state machine, and if so,
use it's current axes to plot the columns values to it. This works as
expected.
However in the second case, the data consists of two columns. There
are several options how to handle such a plot, including using
different subplots with shared or non-shared axes etc. In order for
pandas to be able to apply any of those possible requirements, it will
by default create a new figure to which it can add the axes to plot
to. The new figure will not know about the already existing figure and
its size, but rather have the default size, unless you specify the
figsize argument.

Categories