我无法显示pyplot创建的.png文件。我在Microsoft Azure Jupyter Notebook中创建了该文件。 os.listdir()返回xx.png,所以我知道该文件已创建。然而,显示(图像(“xx.png”))不显示图像。我已经阅读了有关stackoverflow的十个相关帖子,并尝试了该命令的多种变体,但没有任何作用。
当我在本地计算机上重现问题时,它可以正常工作。
这个问题重写了之前被标记为重复并留下死亡的问题。我希望这篇文章能让这个问题更容易理解。
以下是用于创建文件的代码:
from IPython.display import display
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
y = [2,4,6,8,10,12,14,16,18,20]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
fig.savefig('xx.png')
答案 0 :(得分:3)
给出显示图像的代码有两种方法。
1)您可以在笔记本顶部拨打%matplotlib inline
。这将是内嵌图形,您将通过调用fig
2)您可以from IPython.display import Image
和Image('xx.png')
并显示图片。
答案 1 :(得分:1)
您似乎缺少 package com.example.a2118667.red;
//imports
import android.content.Intent;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.widget.TextView;
import java.util.Random;
import static com.example.a2118667.red.R.id.startButton;
public class MainActivity extends AppCompatActivity {
final int[] cowbears = {0};
final int[] cowbears2 = {0};
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView myText = (TextView) findViewById(R.id.indicatorText);
final Button myButton = (Button) findViewById(R.id.startButton);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
myButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
((ViewGroup) myButton.getParent()).removeView(myButton);
colourSwitch();
}
}
);
button4.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
myText.setText("cowBears rock");
// if button says yellow and button is yellow
if (cowbears[0] == 2 && cowbears2[0] <= 1 || cowbears[0] == 6 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] == 3 && cowbears2[0] == 2 || cowbears[0] == 7 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] == 4 && cowbears2[0] == 3 || cowbears[0] == 8 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] <= 1 && cowbears2[0] == 4 || cowbears[0] == 5 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
button3.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
myText.setText("cowBears rock");
// if button says yellow and button is yellow
if (cowbears[0] == 3 && cowbears2[0] <= 1 || cowbears[0] == 8 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] == 4 && cowbears2[0] == 2 || cowbears[0] == 5 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] <= 1 && cowbears2[0] == 3 || cowbears[0] == 6 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] == 2 && cowbears2[0] == 4 || cowbears[0] == 7 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
button2.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
myText.setText("cowBears rock");
// if button says yellow and button yellow
if (cowbears[0] == 4 && cowbears2[0] <= 1 || cowbears[0] == 7 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] <= 1 && cowbears2[0] == 2 || cowbears[0] == 8 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] == 2 && cowbears2[0] == 3 || cowbears[0] == 5 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] == 3 && cowbears2[0] == 4 || cowbears[0] == 6 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
// onClick Listeners for buttons
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
//debugged
myText.setText("cowBears rock");
// if button says yellow and button yellow
if (cowbears[0] <= 1 && cowbears2[0] <= 1 || cowbears[0] == 5 && cowbears2[0] <= 1) {
myText.setText("You did it, mommy!");
}
// if button says blue and button blue
if (cowbears[0] == 2 && cowbears2[0] == 2 || cowbears[0] == 6 && cowbears2[0] == 2) {
myText.setText("You did it, mommy!");
}
// if button says green and button green
if (cowbears[0] == 3 && cowbears2[0] == 3 || cowbears[0] == 7 && cowbears2[0] == 3) {
myText.setText("You did it, mommy!");
}
// if button says red and button red
if (cowbears[0] == 4 && cowbears2[0] == 4 || cowbears[0] == 8 && cowbears2[0] == 4) {
myText.setText("You did it, mommy!");
}
String verify = myText.getText().toString();
if (verify.equals("cowBears rock")){
losedThegameBoi();
}
String check = myText.getText().toString();
if (check.equals("You did it, mommy!")) {
colourSwitch();
}
}
}
);
}
private void losedThegameBoi() {
Intent intent = new Intent(this, Loser_Activity.class);
startActivity(intent);
}
private void colourSwitch() {
final TextView myText = (TextView) findViewById(R.id.indicatorText);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final int[] cowBears = {0};
final int[] cowBears2 = {0};
final int[] cowbears = {0};
final int[] cowbears2 = {0};
//Random number generator for randomizing colour layout
Random randomGenerator = new Random();
//For generating what colour pattern to be displayed
int randomInt = randomGenerator.nextInt(9);
myText.setText("Generated : " + randomInt);
cowBears[0] = randomInt;
cowbears[0] = randomInt;
//Colour patterns based on what integer is generated
//Random number generator for randomizing button click stuff
Random randomGenerator2 = new Random();
//For generating which text to be displayed
int randomInt2 = randomGenerator2.nextInt(5);
myText.setText("Generated : " + randomInt2);
cowBears2[0] = randomInt2;
cowbears2[0] = randomInt2;
// if statements for the colour scheme
if (cowBears[0] <= 1) {
button1.setBackgroundResource(R.drawable.yellow);
button2.setBackgroundResource(R.drawable.blue);
button3.setBackgroundResource(R.drawable.green);
button4.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 2) {
button4.setBackgroundResource(R.drawable.yellow);
button1.setBackgroundResource(R.drawable.blue);
button2.setBackgroundResource(R.drawable.green);
button3.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 3) {
button3.setBackgroundResource(R.drawable.yellow);
button4.setBackgroundResource(R.drawable.blue);
button1.setBackgroundResource(R.drawable.green);
button2.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 4) {
button2.setBackgroundResource(R.drawable.yellow);
button3.setBackgroundResource(R.drawable.blue);
button4.setBackgroundResource(R.drawable.green);
button1.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 5) {
button1.setBackgroundResource(R.drawable.yellow);
button3.setBackgroundResource(R.drawable.blue);
button2.setBackgroundResource(R.drawable.green);
button4.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 6) {
button4.setBackgroundResource(R.drawable.yellow);
button1.setBackgroundResource(R.drawable.blue);
button3.setBackgroundResource(R.drawable.green);
button2.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 7) {
button2.setBackgroundResource(R.drawable.yellow);
button4.setBackgroundResource(R.drawable.blue);
button1.setBackgroundResource(R.drawable.green);
button3.setBackgroundResource(R.drawable.download);
}
if (cowBears[0] == 8) {
button3.setBackgroundResource(R.drawable.yellow);
button2.setBackgroundResource(R.drawable.blue);
button4.setBackgroundResource(R.drawable.green);
button1.setBackgroundResource(R.drawable.download);
}
//Here we have the randomizing of the button text based on what integer is genetated for cowbears2
if (cowBears2[0] <= 1) {
myText.setText("yellow" + cowBears[0] + cowBears2[0]);
}
if (cowBears2[0] == 2) {
myText.setText("Blue" + cowBears[0] + cowBears2[0]);
}
if (cowBears2[0] == 3) {
myText.setText("Green" + cowBears[0] + cowBears2[0]);
}
if (cowBears2[0] == 4) {
myText.setText("Red" + cowBears[0] + cowBears2[0]);
}
}
}
语句,请尝试:
import