This question already has answers here:
How to adjust padding with cutoff or overlapping labels
(8 answers)
Closed 4 years ago.
So I'm using yellowbrick in Python, which is basically matplotlib and scikit-learn combined, to visualize some data.
My chart looks like this:
The labels get cut off. What I want to do is to adjust the figure so the labels on the right don't get cut off. I tried
plt.rcParams['figure.figsize'] = (10, 5)
plt.rcParams['font.size'] = 12
but when I rendered the figure, it's still cut off. Even when I save it as a png file it's still cut off. What am I missing here?
tight_layout method should solve your problem.
Generally you can use it with:
fig.tight_layout() # if fig is your figure handle
or
plt.tight_layout() # if stated within the context of your figure
This line of code should be added after the last plotting statement just before rendering the figure.
If this does not work, please post a fully working minimal code example, as described in mcve. Afterwards I'll be able to post a fully working solution for most, if not all, cases.
Related
This question already has answers here:
How to add hovering annotations to a plot
(12 answers)
Closed 3 years ago.
How can I plot a figure in Python like MATLAB with the following features?:
1) I can zoom in and zoom out.
2) I can modify the range for x and y on the figure
3) I can click on the figure to see actual numbers related to each data point
Another question, how can we specify with wath resolution the matplotlib saves the figure?
To the best of my knowledge, MATPLOTLIB does not have that. Anything else?
I would recommend looking at plotly.
Plotly allows you to make graphs that can be
1) zoomed in on
2) change the x and y-axis numbers by dragging on the axes (but cant switch from say, log scale to linear scale as easily -- something like this would require an interaction feature)
3) Plotly allows you to display info on cursor-hover
For an example of a plotly plot in use, see this washington post page.
This question already has answers here:
matplotlib: can I create AxesSubplot objects, then add them to a Figure instance?
(5 answers)
pyplot - copy an axes content and show it in a new figure
(2 answers)
Closed 3 years ago.
When I want to make several subplots in a figure I can do, for example, the following
figA=plt.figure('figA',figsize=(30,25))
(ax2,ax1) = figA.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
And I can then make the plot using ax2.plot(data2) and ax1.plot(data1)
Then I might want to do another separate figure:
figB=plt.figure('figB',figsize=(30,25))
(ax2B,ax1B) = figB.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
However, I need the exact same top panel as in the previous figure.
What should I do if I want ax2B to always be identical to ax2, no matter what changes I make to the subplot?
In other words, I would like to define a subplot and apply it to several figures, rather than defining it inside a specific figure.
If for example in the top panel I want a straight line f(x)=x, I will do
import numpy as np
X=np.linspace(0.,10.,10)
ax2.plot(X,X)
ax2B.plot(X,X)
but I do not want to define the exact same plot twice. I want to just define a subplot once and for all and then call it in a new figure when I need it.
This question already has answers here:
How to remove gaps between subplots in matplotlib
(6 answers)
Closed 5 years ago.
I am attempting to plot a graph similar to the one attached below. The y-axis should be aligned and the x-axis scale and name should be same. Also, I want the dotted line as shown in the figure.
I tried combining two different graphs but that is certainly not a good way to solve this.
Sorry for the poor quality picture but the important points are mentioned.
Look into the matplotlib docs. I think you might be looking to use plt.subplots with the sharex argument.
This question already has answers here:
Plotting different colors in matplotlib [duplicate]
(3 answers)
Closed 5 years ago.
I have a pretty short python program to map my RAM usage over time.
while i < 1000000000000:
x.append(i);
y.append(psutil.virtual_memory().used);
plt.plot(x,y)
i+=1;
plt.show()
plt.pause(0.0001)
For some reason, this graph changes color every time a new data point is added.
Does this have anything to do with the plt.ion() I have? It also re-opens every time I close it. Do you guys have any solutions? Thanks in advance!
I figured it out!
The color (my primary problem) I could fix by adding a color="black" to the plt.plot() line. My code looked like this:
plt.plot(x,y,color="black")
I wasn't able to figure out the thing where it doesn't close, but that's okay, I can still close it from the task manager.
This question already has answers here:
Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig
(6 answers)
Closed 1 year ago.
I am attempting to create a dataframe histogram and save it as a file.
Here is my code:
ax=df.hist('ColumnName')
fig=ax.get_figure()
fig.savefig('pictureName.png', dpi=100, bbox_inches='tight')
The first line works fine; however, the second line returns an error:
AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'.
Because this question shows the get_figure() being applied to series.hist(), I have also tried using ax=df['ColumnName'].hist(), which successfully produced a histogram but led to the same error message when I attempted to implement get_figure().
As recommended in this other question, normally I would skip the get_figure() and the fig.savefig(), opting instead for plt.savefig, but I am making multiple figures. In my experience, plt.savefig() is unreliable in saving multiple figures, instead saving one figure multiple times, even when I use fig.close() after each figure creation and save.
I very much want to solve this problem as neatly as possible, so that I can carry the solution smoothly into other applications, rather than having to use a different duct-tape fix every time I have to make a graph.
Thank you for your help!
Can you try the following code?
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df.hist('ColumnName', ax=ax)
fig.savefig('example.png')