plt.show and plt.savefig give different result
You can use:
plt.show()
before
plt.savefig("../res/dense.png",dpi=plt.gcf().dpi)
It's just a matter of convention. When we call plt.savefig
the reference of all the variable point till plt.savefig
after that any statement it corresponds to new plot.
You can accept the answer. If it helps.
Different plot size for plt.show() and fig.savefig()
Tangential note: you shouldn't from pylab import *
, that will clutter your namespace with all sorts of needless things:
>>> len(globals())
6
>>> from pylab import *
>>> len(globals())
988
As you noticed, I don't think that setting any of the rc
parameters affects existing plots. You might have to get your hands dirty, although only a bit: there is only a handful of children of your Axes
that you need to modify:
import matplotlib as mpl
import matplotlib.pyplot as plt # <-- all you need here
params = {
'text.usetex': True,
'font.size': 6,
}
mpl.rcParams.update(params)
fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(111)
x = [1, 2, 3]
y = [2, 2, 2]
ax.plot(x,y, label='label')
plt.tight_layout()
fig.savefig('plot.pdf')
fig.set_size_inches(10, 8, forward=True)
# v--- change title and axeslabel font sizes manually
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(20)
plt.show()
Saved (left) and shown (right) version:
Arguably, it might be more elegant (or a different kind of messy) to just close your saved plots, modify your rcParams
, and redraw the versions you want to show
.
Plt.show shows full graph but savefig is cropping the image
You may try
plt.savefig('X:/' + newName + '.png', bbox_inches='tight')
Or you may define figure size like
fig = plt.figure(figsize=(9, 11))
...
plt.savefig(filename, bbox_inches = 'tight')
Matplotlib: Different views from plt.savefig() and manual saving
The following gives me very similar appearance in "show
" and in "savefig
". I rearranged your code just to help me look at it, but I believe there are two differences in mine that may help yours: Defining the figure size to make sure it has room for the legends, and using bbox_inches="tight"
in the savefig
call.
import matplotlib.pyplot as plt
plt.style.use('ggplot')
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2, figsize=(11,10))
labels1 = ('new', 'closed', 'developing', 'testing', 'tbd')
labels2 = ('Intern', 'Kunde')
labels3 = ('HW', 'SW')
labels4 = ('Closed', 'Open')
sizes1 = [1, 2, 3, 4, 5]
sizes2 = [1,2]
sizes3 = [1,2]
sizes4 = [1,2]
colors1 = ['#F5FC34', '#52FC34', '#FAAB23', '#237DFA', '#C863FA', ]
colors2 = ['#FAF063', '#63BBFA']
colors3 = ['#6A0D7D', '#503C3C']
colors4 = ['#58FB4C', '#F8183A']
explode1 = (0, 0, 0, 0, 0)
explode2 = (0, 0)
explode3 = (0, 0)
explode4 = (0, 0)
ax1.pie(sizes1, explode=explode1, colors=colors1, autopct='%1.1f%%',shadow=True, startangle=90)
ax2.pie(sizes2, explode=explode2, colors=colors2, autopct='%1.1f%%',shadow=True, startangle=90)
ax3.pie(sizes3, explode=explode3, colors=colors3, autopct='%1.1f%%',shadow=True, startangle=90)
ax4.pie(sizes4, explode=explode4, colors=colors4, autopct='%1.1f%%',shadow=True, startangle=90)
ax1.set_title("Status")
ax2.set_title("Department")
ax3.set_title("Origin")
ax4.set_title("Closed-Status")
ax1.legend(labels1, bbox_to_anchor=(0.05, 0.8))
ax2.legend(labels2, bbox_to_anchor=(1.25, 0.65))
ax3.legend(labels3, bbox_to_anchor=(0, 0.6))
ax4.legend(labels4, bbox_to_anchor=(1.25, 0.65))
plt.show()
fig.savefig('/Users/ite1/Desktop/test.png', bbox_inches="tight")
Related Topics
Python How to Write to a Binary File
Call a Python Function from Jinja2
How to Access Pandas Groupby Dataframe by Key
Make a Post Request While Redirecting in Flask
File Not Found Error When Launching a Subprocess Containing Piped Commands
Check If String Has Date, Any Format
How to Do a Not Equal in Django Queryset Filtering
Python Convert Tuple to String
Use Pytesseract Ocr to Recognize Text from an Image
Save/Load Scipy Sparse Csr_Matrix in Portable Data Format
Conda' Is Not Recognized as Internal or External Command
Matplotlib: How to Plot Images Instead of Points
Boto3 to Download All Files from a S3 Bucket
How to Automatically Fix an Invalid JSON String
What's the Best Way to Generate a Uml Diagram from Python Source Code